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?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
In C++03 you are not guaranteed that
insert
anderase
preserve relative ordering. However, this is changed in C++0x: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. :/
Taking into account that
a1
anda2
would compare equal in your example, and what is actually stored in thestd::multiset
are copies ofa1
anda2
, I don't really know how would you know which is which.If you can tell the difference, maybe
class A
was not well designed in the first place. Sostd::multiset
does not guarantee such a thing.std::multimap does not guarante this. If you can express your
operator<
using an integer via a function e.g.int A::orderingInt()
, you could use awith
with overloaded
Of course adding and iteration would be different now: