std::map 中的元素是否保证有序?
这只是一个实现副作用(红黑树)还是c++标准保证了顺序?
Is this just an implementation side effect (red-black tree) or the order is guaranteed by the c++ standard?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有序迭代不是实现细节;它由 C++ 标准保证。它是所有关联容器的基本属性 (C++03 §23.1.2/9):
value_comp
是构造映射所用的比较器(默认情况下,它是std::less
)。Ordered iteration is not an implementation detail; it is guaranteed by the C++ standard. It is a fundamental property of all associative containers (C++03 §23.1.2/9):
value_comp
is the comparator with which the map was constructed (by default, it isstd::less<T>
).§23.1.2/2:
默认的Compare对象是小于函数std::less。
顺序是函数的一个属性。这是一个要求,而不是副作用。
对对象进行排序是一个副作用。 23.1.2/10 和 23.1.2/9(James 引用)保证在从
begin
到的序列中,map/set 和 multimap/multiset 分别具有递增/非递减键>结束
。§23.1.2/2:
The default
Compare
object is the less-than functionstd::less<Key>
.The ordering is a property of the function. It's a requirement, not a side effect.
Sorting the objects is a side effect. 23.1.2/10 and 23.1.2/9 (quoted by James) guarantee that map/set and multimap/multiset have increasing/non-decreasing keys, respectively, over the sequence from
begin
toend
.它由 C++ 标准保证。
It's guaranteed by the c++ standard.
有保证。如果您想要不受此限制的东西,请尝试 boost::unordered_map<>
Guaranteed. If you want something that's not constrained by this try boost::unordered_map<>