【发布时间】:2020-05-03 00:45:24
【问题描述】:
矢量擦除功能会导致错误,而清除功能有效。 这是什么原因..?
#include <algorithm>
#include <vector>
#include <iostream>
struct person_id{
person_id() = default;
person_id(int id) : p_id (id) {}
bool operator==(const person_id& other) { return p_id == other.p_id; }
int p_id;
};
using std::cout;
using std::endl;
int main(int argc, char* argv[]) {
std::vector<person_id> people;
person_id tmp_person;
tmp_person.p_id = 5;
people.push_back(tmp_person);
people.erase(5); // error : “No matching function for call 'erase'
people.clear(); // works
return 0;
}
【问题讨论】:
-
请改用
people.erase(people.begin() + 0);。您不能在std::vector::erase中提供密钥(例如在std::set中)。只接受迭代器。
标签: c++