std::map Requirements for Keys (Design Decision)

后端 未结 5 1211
天涯浪人
天涯浪人 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:11

    The reason why a comparison operator is needed is the way map is implemented: as a binary search tree, which allows you to look up, insert and delete elements in O(log n). In order to build this tree, a strict weak order must be defined for the set of keys. That's why only one operator definition is needed.

提交回复
热议问题