如何使用set包含类对象的问题

b_horse 2007-02-28 03:47:04
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的一致性。
不知道有谁知道上面的代码问题在哪里!?
先谢谢了
...全文
378 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
sinall 2007-02-28
  • 打赏
  • 举报
回复
以_a的值排序,以_b的值保证set中值的唯一性
<=>
以_b的值保证set中值的唯一性
<=>
bool operator<(const A& c) const { return _b < c._b; }

不知道楼主何来“_a的值排序”的问题?
b_horse 2007-02-28
  • 打赏
  • 举报
回复
class ACmp
{
public:
bool operator()(const A& left, const A& right) const
{
return left._b!=right._b && left._a < right._a;
}
};
是正解!
jixingzhong 2007-02-28
  • 打赏
  • 举报
回复
set 无法插入重复元素 ~
jixingzhong 2007-02-28
  • 打赏
  • 举报
回复
使用 multiset ......
sinall 2007-02-28
  • 打赏
  • 举报
回复
定义operator的目的是,想用set容器,以_a值排序,以_b值保持set中元素的唯一性。但好像结果出乎意料:
——————————————————————————————————————————
貌似只和operator<相关。另外,不存在所谓排序的问题,只存在保证唯一性的问题。
也就是说,set容器插入元素的时候,根据operator<来判断2个元素是否相同,即:
!(a < b) && !(b < a)

而你的if (a1 == a3)则是调用operator==,二者原理完全不同!
taodm 2007-02-28
  • 打赏
  • 举报
回复
看《Effective STL》item 19
b_horse 2007-02-28
  • 打赏
  • 举报
回复
纠正一下,如果定义是这样的话,(以_a的值排序,以_b的值保证set中值的唯一性)
bool operator<(const A& b) const { return _a < b._a; }
bool operator==(const A& b) const { return _b == b._b ; }

且有
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);

输出为:
1 and hehe
2.01 and hehe
a1 == a3
比较奇怪,因为,a1和a3是相等的,可见==在向set中插值时,并没起作用。

acange 2007-02-28
  • 打赏
  • 举报
回复
你可以修改你的< == operator, 使他比较是也考虑 _b 这个成员.
bool operator<(const A& b) const { return ... }
bool operator==(const A& b) const { return ... }
acange 2007-02-28
  • 打赏
  • 举报
回复
这里问题在于你在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

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧