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

来源:互联网 发布:nba2k17人物数据 编辑:程序博客网 时间:2024/05/17 08:21
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.

原创粉丝点击