hash_map
was a common extension provided by many library implementations. That is exactly why it was renamed to
unordered_map
when it was added to the C++ standard as part of TR1. map is generally implemented with a balanced binary tree like a red-black tree (implementations vary of course).
hash_map
and unordered_map
are generally implemented with hash tables. Thus the order is not maintained.
unordered_map
insert/delete/query will be O(1) (constant time) where map will be O(log n) where n is the number of items in the data structure. So
unordered_map
is faster, and if you don't care about the order of the items should be preferred over
map
. Sometimes you want to maintain order (ordered by the key) and for that
map
would be the choice.
發佈了7 篇原創文章 · 獲贊 0 · 訪問量 1萬+