std::map, custom comparator's design constraints

后端 未结 3 1094
一个人的身影
一个人的身影 2021-01-23 11:56

I have been trying defining a custom comparator for std::map container.

The question is : can I relay on == and != operators or would that break strict weak order ? Am I

3条回答
  •  情书的邮戳
    2021-01-23 12:11

    You may not care how things are ordered in the map, but the map does. The code you've provided above does not appear to implement strict weak ordering correctly. As you have already noted that switching left with right will not change the result of operator() in all cases, your function does not implement strict weak ordering.

    You do not necesarrily have to implement the comparator in terms of operator< directly, but you must ensure that if operator()(A,B) returns true, then operator()(B,A) does not also return true.

提交回复
热议问题