What is the time complexity of std::map

后端 未结 4 1922
野趣味
野趣味 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:13

    if you're asking about the lookup time compexity for std::map then that would be O(log(n)) since the stl implemented the std::map library as a Tree (binary tree) datastructure.

    More information here: http://www.cplusplus.com/reference/map/map/

    Here is also the commonly used std::unordered_map, which would be your hashmap (no ordering hence) with a O(1) look up speed, however, using more memory than the Tree per the design of the data structure.

提交回复
热议问题