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错误:二进制'==':没有找到采用'g'类型的左操作数的运算符。
答案 0 :(得分:1)
您应该为您的课程实施==
运算符:
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});
答案 1 :(得分:1)
您可以使用lambda和标准算法std::find_if
。
如果我正确理解了您的代码,则需要类似
int x = 2;
auto it = find_if( f.begin(),
f.end(),
[x]( const g &item )
{
return item.x == x || item.y == x || item.z == x;
} );