std :: multiset是否保证插入订单? [英] Does std::multiset guarantee insertion order?
问题描述
我有一个 std :: multiset
,它存储 class A
的元素。我为这个类提供了我自己实现的 operator<
。我的问题是如果我将两个等效的对象插入到这个多集群中,那么他们的订单是否有保证?例如,首先我将一个对象 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
preserve 相对排序。但是,这是在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类支持等效的键。 对于多集和多重映射,插入和删除保留等效元素的相对顺序。 重点是我的。
这个缺陷报告。 此页是评论的集合在这个问题上,它的写作非常完整,相当丰富。 (我非常建议在以前的概述链接中阅读这个。)
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屋!