读书人

怎么使用set包含类对象的有关问题

发布时间: 2012-03-03 15:33:03 作者: rapoo

如何使用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的一致性。
不知道有谁知道上面的代码问题在哪里!?
先谢谢了

[解决办法]
这里问题在于你在class A 中定义:
bool operator <(const A& b) const { return _a < b._a; }
bool operator==(const A& b) const { return _a == b._a; }

你是根据_a (double)的大小来比较A 的对象的大小, 当你定义 a1 a2 a3后,并插入到a_set 中时,你的a2没有被插入, 这是因为a3._a = a2._a. 所以在a_set中只有2 个元素.
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); //a2没有被插入


所以你的输出是这样的.
输出:
1 and hehe
2.01 and hehe
[解决办法]
看《Effective STL》item 19
[解决办法]
定义operator的目的是,想用set容器,以_a值排序,以_b值保持set中元素的唯一性。但好像结果出乎意料:
——————————————————————————————————————————
貌似只和operator <相关。另外,不存在所谓排序的问题,只存在保证唯一性的问题。
也就是说,set容器插入元素的时候,根据operator <来判断2个元素是否相同,即:
!(a < b) && !(b < a)

而你的if (a1 == a3)则是调用operator==,二者原理完全不同!
[解决办法]
使用 multiset ......

读书人网 >C++

热点推荐