class g {
public:
int x, y, z;
};
vector<g>f;
int main() {
g obj1 = { 1,2,3 };
f.push_back(obj1);
auto it = find(f.begin(), f.end(), 2);
f.erase(it);
}
这段代码给我一个 C2678 错误:binary '==': no operator found which takes a left-hand operand of type 'g'。
最佳答案
你应该为你的类实现==
操作符:
class g {
public:
int x, y, z;
bool operator==(const g& o) const {
return x == o.x && y == o.y && z == o.z;
}
};
使用正确的类型作为参数:
auto it = std::find(f.begin(), f.end(), g{1, 2, 3});
关于c++ - 如何使用 std::find 作为类 vector ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59501380/