What is the time complexity of std::map

后端 未结 4 1912
野趣味
野趣味 2020-12-19 03:00

What is the time complexity of std::map? And will it degenerate in the worst case? Or it is decide by the implementation, we can\'t know it?

4条回答
  •  粉色の甜心
    2020-12-19 03:30

    Usually the time complexity is specified for operations. In your case the lookup and insert seems relevant.

    See http://www.sgi.com/tech/stl/complexity.html for the guaranteed complexties of the STL containers. And this http://www.sgi.com/tech/stl/AssociativeContainer.html on the Associative Containers.

    Another source is found here:

    • http://www.cplusplus.com/reference/map/map/find/ --> Logarithmic in size.
    • http://www.cplusplus.com/reference/map/map/insert/ --> If a single element is inserted, logarithmic in size in general, but amortized constant if a hint is given and the position given is the optimal.

    the lookup of std::map is log(number of elements in the map).

提交回复
热议问题