11

I am wondering whether this is true? If it is, is this behavior guaranteed by the c++ standard?

James McNellis
  • 348,265
  • 75
  • 913
  • 977
Thomson
  • 20,586
  • 28
  • 90
  • 134
  • Duplicate of [Does std::multiset guarantee insertion order?](http://stackoverflow.com/questions/2643473/does-stdmultiset-guarantee-insertion-order) – James McNellis Jul 28 '10 at 12:52

1 Answers1

17

The elements in a std::map must have unique keys, so... no.

The std::multimap container allows multiple values mapped to one key. When iterating over a std::multimap the elements are ordered by key, but the order of elements having the same key is not specified.

Note that in the latest draft of the forthcoming C++0x standard (N3092), the relative ordering of elements with the same key is guaranteed (so, at some point, you'll be able to rely on this behavior).

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • Thanks. I mean multimap in my previous post. – Thomson Jul 28 '10 at 03:23
  • 2
    this statement seems different from the last proposed new standard that I have (but that is 21 months old (N2798=08-0308)) I quote: For multiset and multimap, insert and erase preserve the relative ordering of equivalent elements. page 768 If a range containing elements equivalent to t exists in a_eq, t is inserted at the end of that range. page 771 – pgast Jul 28 '10 at 04:30
  • @pgast: Very interesting. That language is in the C++0x FCD (N3092). However, it is not the case in the current C++ standard (C++03) that the relative ordering is maintained. Thanks for pointing out that change. – James McNellis Jul 28 '10 at 12:43
  • 3
    @PhilLello: C++0x was later named C++11, so yes, the ordering is preserved in C++11. – John Zwinck Jan 30 '15 at 02:37