0

Not the usual question, I hope.

So prior to C++ 11, there are no guarantees that insert ordering is respected. That much I understand (e.g., http://www.cplusplus.com/reference/map/multimap/insert/ and how does the stl's multimap insert respect orderings?).

Is non-guarantee warning just regarding portability between versions and platforms?

Or does it mean it's possible that a seemingly well-behaved implementation can jumble its equal-key ordering under particular circumstances? And f the latter, is there a way to force this behavior?

Thanks.

Community
  • 1
  • 1
hythlodayr
  • 2,377
  • 15
  • 23

1 Answers1

1

At least on some implementations--confirmed this on GNU C++, at least--this can happen when using hints.

Using a modified version of the example code in http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1780.html

typedef std::multimap<int, std::string> Map;
typedef std::pair<int, std::string> value_type;
Map m;
m.insert(value_type(0, "zero"));
m.insert(value_type(1, "one"));
m.insert(value_type(2, "two"));
display(m); // Iterates from m.begin to m.end in order and shows key/value pair.
m.insert(m.find(1), value_type(1, "ONE"));

std::pair<Map::iterator, Map::iterator> range;
range = m.equal_range(1);
Map::iterator it;

it = range.first; 
it++;

m.insert(it, value_type(1, "ONE_MID")); // Try inserting between "one" and "ONE"
display(m);

On GCC 4.6.3, this displays:

(0, zero) (1, one) (2, two)
(0, zero) (1, ONE) (1, ONE_MID) (1, ONE) (2, two)

Which obviously isn't the insert order!

hythlodayr
  • 2,377
  • 15
  • 23