問題描述
既然 std
在 unordered_map
中有一個真正的哈希映射,為什么(或何時)我還想使用舊的 map
unordered_map
在它實際存在的系統上?是否有任何我無法立即看到的明顯情況?
Now that std
has a real hash map in unordered_map
, why (or when) would I still want to use the good old map
over unordered_map
on systems where it actually exists? Are there any obvious situations that I cannot immediately see?
推薦答案
As 已經提到,map
允許以排序的方式迭代元素,但unordered_map
不允許.這在許多情況下非常重要,例如顯示集合(例如地址簿).這也體現在其他間接方式上,例如:(1) 從 find()
返回的迭代器開始迭代,或 (2) 存在像 lower_bound()
這樣的成員函數.
As already mentioned, map
allows to iterate over the elements in a sorted way, but unordered_map
does not. This is very important in many situations, for example displaying a collection (e.g. address book). This also manifests in other indirect ways like: (1) Start iterating from the iterator returned by find()
, or (2) existence of member functions like lower_bound()
.
此外,我認為在最壞情況 搜索復雜性方面存在一些差異.
Also, I think there is some difference in the worst case search complexity.
對于
map
,是O(lg N)
對于unordered_map
,它是O( N ) [當哈希函數不好導致太多哈希沖突時,可能發生這種情況.]
For unordered_map
, it is O( N ) [This may happen when the hash function is not good leading to too many hash collisions.]
同樣適用于最壞情況 刪除復雜性.
這篇關于在 std::map 和 std::unordered_map 之間進行選擇的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!