std::map Requirements for Keys (Design Decision)

后端 未结 5 1202
天涯浪人
天涯浪人 2021-02-13 18:24

When I make a std::map, what C++ expects from me is that my_data_type has its own operator<.

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-13 19:13

    In order to find the element i within the map, we have traversed to element e the tree search will already have tested i < e, which would have returned false.

    So either you call i == e or you call e < i, both of which imply the same thing given the prerequisite of finding e in the tree already. Since we already had to have an operator< we don't rely on operator==, since that would increase the demands of the key concept.

提交回复
热议问题