Choosing between std::map and std::unordered_map(在 std::map 和 std::unordered_map 之间进行选择)
问题描述
既然 std 在 unordered_map 中有一个真正的哈希映射,为什么(或何时)我还想使用旧的 mapunordered_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 之间进行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 std::map 和 std::unordered_map 之间进行选择
基础教程推荐
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
