您的位置:首页 > 产品设计 > UI/UE

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

2017-08-28 12:00 357 查看
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐