什么是实现自定义 find() 函数的干净方法?例如,我希望我的 operator== 为类 X 工作,匹配接近现有值的变量值。
class X{
public:
double _a;
double _b;
double _c;
X(double a, double b, double c){
_a = a;
_b = b;
_c = c;
}
bool operator==(const X& other) const
{
if(fabs(other._a - _a) < 0.02) return true;
return false;
}
};
typedef X* ptrX;
std::vector<ptrX> vec;
ptrX t1 = new X(1,2,3);
vec.push_back(t1);
ptrX t = new X(1.01,2,3);
bool b = (find(vec.begin(),vec.end(),t) == vec.end()); //b should be false