键值关联容器中的顺序是有用的用例是什么
what is the uses cases in which order in key value associative container is useful
我想知道在哪些用例中,我们可以通过拥有有序的关联容器来感兴趣。
换句话说,为什么使用std::map
而不使用std::unorderd_map
当您需要能够以有序的方式迭代键时,可以使用有序映射。
使用map
而不是unordered_map
的主要原因是map
是标准的,您一定有。一些公司有政策不使用tr1
中的定义
但您会问订单什么时候很重要——lower_bound
、upper_bound
、set_union
、set_intersection
等操作都需要订单。
如果需要在无序容器之间进行比较可能是个问题operator==
/operator!=
在无序容器
N3290§23.2.5/11称
运算符的复杂性==。。。是与…成比例。。。N^2在最坏的情况下case,其中N是a.size().
而其他容器具有线性复杂性。
Unordered_map比map使用更多内存。如果内存中存在约束,则建议使用贴图。因此,当使用无序映射时,操作会变慢
编辑
引用维基文章
It is similar to the map class in the C++ standard library but has different
constraints. As its name implies, unlike the map class, the elements of an
unordered_map are not ordered. This is due to the use of hashing to store objects.
unordered_map can still be iterated through like a regular map.
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- C++避免重复声明的语法是什么
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- C++中名称篡改的目的是什么
- 在 c++ 中拥有一组结构的正确方法是什么?
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 是什么阻止DOMTimerCoordinator::NextID进入无休止的循环
- 派生类销毁的最佳实践是什么
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 通过JNI传递数据数组的最快方法是什么
- "using namespace std;"在C++的作用是什么?
- 在两台机器之间进行时间戳的最佳c++chrono函数是什么
- 文件系统:复制功能的速度秘诀是什么
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 在不编写显式 setter 的情况下修改私有类数据成员的便捷方法是什么?模板有用吗?
- 是什么让这种图像处理有用[加速C++]
- 键值关联容器中的顺序是有用的用例是什么