Nov 4, 2009 at 2:57am UTC
The following code is cropped from my project. please help me to find why the multiset fail to find the match. The element (51,142) is supposed to be matched with (54,142) following the operator==() defined in the class. Thanks for any hint of solution.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include <cmath>
#include <cstdlib>
#include <set>
#include <utility>
#include <iostream>
using namespace std;
class test {
pair<int ,int > pos;
public :
test(int x,int y):pos(x,y){}
friend bool operator ==(const test & t1,const test & t2) {
return abs(t1.pos.first - t2.pos.first) <= 8 &&
abs(t1.pos.second - t2.pos.second) <= 8;
}
friend bool operator <(const test & t1,const test & t2) {
if (t1 == t2) return false ;
else if (abs(t1.pos.first - t2.pos.first) <= 8) return t1.pos.second < t2.pos.second;
else if (abs(t1.pos.second - t2.pos.second) <= 8) return t1.pos.first < t2.pos.first;
else //if(!(abs(t1.pos.x - t2.pos.x) < 8) && !(abs(t1.pos.y - t2.pos.y) < 8))
return t1.pos.first < t2.pos.first;
}
void print() const {
cout<<pos.first<<" " <<pos.second<<endl;
}
};
int main()
{
multiset<test> ms;
ms.insert(test(50,117));
ms.insert(test(62,129));
ms.insert(test(54,142));
if (ms.end() == ms.find(test(51,142))) cout<<"error" <<endl;
return EXIT_SUCCESS;
}
Last edited on Nov 4, 2009 at 2:59am UTC