map and unordered_map in c++

map and unordered_map in c++

Implementation principle

map : The internal elements of the map are ordered, because the red-black tree is implemented internally and has the function of automatic sorting. Each node of the red-black tree represents an element of the map. Therefore, operating the map is equivalent to operating the red-black tree, and the operation efficiency depends on the efficiency of the red-black tree.
unordered_map : Its internal is a hash table , although it is unordered, the search efficiency is high, which is O(1)

difference

The biggest advantage of map is order . For problems with order requirements, map will be more efficient.
On the contrary, the biggest advantage of unordered_map is that the search speed is fast , but the memory footprint is large (space for time), which is more suitable for problems that require frequent search.

Guess you like

Origin blog.csdn.net/qq_43477024/article/details/112000368