如何使用set包含类对象的问题
class定义如下:
class A
{
double _a;
string _b;
public:
A(double a, string b) : _a(a), _b(b){}
bool operator<(const A& b) const { return _a < b._a; }
bool operator==(const A& b) const { return _a == b._a; }
friend ostream& operator<<(ostream& os, const A& a)
{
os << a._a << " and " << a._b << endl;
return os;
}
};
定义operator的目的是,想用set容器,以_a值排序,以_b值保持set中元素的唯一性。但好像结果出乎意料:
程序如下:
A a1(1, "hehe");
A a2(2.01, "haha");
A a3(2.01, "hehe");
set<A> a_set;
a_set.insert(a1);
a_set.insert(a3);
a_set.insert(a2);
copy(a_set.begin(), a_set.end(), ostream_iterator<A>(cout, ""));
if (a1 == a3)
{
cout << "a1 == a3" << endl;
}else
{
cout << "a1 != a3" << endl;
}
输出:
1 and hehe
2.01 and hehe
a1 == a3
如果把class中的operator定义改一下:
bool operator<(const A& c) const { return _b < c._b; }
bool operator==(const A& c) const { return _b == c._b; }
则输出为:
2.01 and haha
1 and hehe
a1 == a3
好像,c++要求operator中比较的量相同时才保证标准里定义的behavior的一致性。
不知道有谁知道上面的代码问题在哪里!?
先谢谢了