Effective STL 21 Always have comparison functions return false for equal values

本文探讨了C++标准库中set与multiset容器的行为差异,特别是在使用less_equal比较器时的情况。对于set而言,尝试插入两个相同值会导致未定义行为;而对于multiset,则可以正确处理并插入多个相同值。通过示例展示了如何使用equal_range来查找multiset中相同值的范围。
set<int, less_equal<int> > s;
s.insert(10);
s.insert(10):

(!(10 <= 10) && !(10 <= 10)) get false. the set concludes that 10 and 10 are not equivalent, hence not the same, and it thus goes about inserting 10 into the container alongside 10. Technically, this action yields undefined behavior.

multiset<int, less_equal<int> > s;
s.insert(10);
s.insert(10);

do an equal_range on it. we’ll get back a pair of iterators that define a range containing both copies.
equal_range dosen’t identify a range of equal values(10 == 10), it identifies a range of equivalent values(!(10 <= 10) && !(10 <= 10)); In this example , s’s comparison function says that 10 and 10 are not equivalent, so there is no way that both can be in the range identified by equal_range.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值