Why is the complexity of the C++ STL map container O(log(n))?

前端 未结 2 848
一整个雨季
一整个雨季 2021-02-20 17:35

For C++ STL containers such as vector and list, the complexity of finding elements and inserting or removing them is self-explanatory. However, for the

2条回答
  •  遥遥无期
    2021-02-20 17:40

    The elements of a map or set are contained in a tree structure; every time you examine a node of the tree, you determine if the element you're trying to find/insert is less than or greater than the node. The number of times you need to do this (for a properly balanced tree) is log2(N) because each comparison throws out half of the possibilities.

提交回复
热议问题