什麼是實現自定義find()函數的乾淨方式?例如,我想讓我的operator ==爲類X找到工作,匹配與現有值接近的變量的值。find()相等運算符過載
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
_Why_你爲什麼使用指針?這個問題完全不需要 – 2012-07-07 21:25:27
另外,[該函數已經存在,在線演示實現](http://www.cplusplus.com/reference/algorithm/find/) – 2012-07-07 21:26:35
除了它會被混淆的事實因爲你使用的是指針而不是值 – 2012-07-07 21:27:37