std :: multiset是否保证插入顺序? [英] Does std::multiset guarantee insertion order?
问题描述
我有一个 std :: multiset
,它存储 A类
的元素。我为这个类提供了自己的运算符<
实现。我的问题是如果我插入两个等效对象到这个multiset是他们的顺序保证?例如,首先我插入一个对象 a1
到集合,然后我插入一个等效对象 a2
到这个集合。当我遍历集合时,我可以预期 a1
在 a2
之前吗?如果没有,是否有任何方法来实现这个使用multiset?
I have a std::multiset
which stores elements of class A
. I have provided my own implementation of operator<
for this class. My question is if I insert two equivalent objects into this multiset is their order guaranteed? For example, first I insert a object a1
into the set and then I insert an equivalent object a2
into this set. Can I expect the a1
to come before a2
when I iterate through the set? If no, is there any way to achieve this using multiset?
推荐答案
在C ++ 03你不能保证 insert
和 erase
保留相对排序。但是,这在C ++ 0x中改变了:
In C++03 you are not guaranteed that insert
and erase
preserve relative ordering. However, this is changed in C++0x:
n3092,§23.2.4/ 4:关联容器支持唯一键对于每个键可以包含至多一个元素。否则,它支持等效键。集合和映射类支持唯一键; multiset和multimap类支持等效键。 对于multiset和multimap,插入和删除会保留等效元素的相对排序。 强调我的。
b $ b
本缺陷报告。 此网页是关于此问题的意见收集,它是精心编写和充实的。 (我非常推荐在之前的概述链接中阅读这个链接。)
This is discussed in this defect report. This page is the collection of comments on the issue, it's well-written and quite fleshed-out. (I very much recommend reading this one over the previous "overview" link.)
从该评论页面,您可以找到当前实现的比较,如果你打算使用的实现遵循你的期望。
From that comment page you'll find a comparison of current implementations, so you can check if the implementations you intend to use follow what you expect.
我不能想到一种强制你想从头顶的顺序。 :/
I can't think of a way to force the ordering you want off the top of my head. :/
这篇关于std :: multiset是否保证插入顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!