Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 map container, even though I know from my reading that the access and insertion complexity/performance is O(log(n)), I can't work out why. I clearly don't understand maps as much as I need to, so some enlightenment on this topic would be very much appreciated.

like image 622
Ed King Avatar asked Jun 27 '12 21:06

Ed King


1 Answers

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.

like image 132
Mark Ransom Avatar answered Sep 22 '22 03:09

Mark Ransom