std::multimap and ordering

Status
Not open for further replies.

Shamyl Zakariya

Ars Praefectus
3,816
Subscriptor++
I recently implemented a simple structure like so:
Code:
std::map< int, std::vector< FOO >>

(I needed the FOO instances added under the same key to retain their order of insertion. )

It occurred to me that this smelled like a use case for std::multimap. I ended up writing some tests and it seems that the objects inserted under the same key as such:
Code:
std::maultimap< int, FOO > mmap;
mmap.insert( std::pair< int, FOO >( 0, foo1 ));
mmap.insert( std::pair< int, FOO >( 0, foo2 ));
mmap.insert( std::pair< int, FOO >( 0, foo3 ));

It seemed that they retained their insertion order when the map was iterated. I tested pretty thoroughly, and it seemed that no sort-ordering magic took place on the inserted FOO instances. So I rewrote my code accordingly, and it seems to work.

My question is, is this implementation-dependent? I'm using the c++ stdlib from Xcode 4.1/llvm/clang. Is this something that will blow up if I use gcc 4.x (or some other compiler) on another platform?

Thanks,
 

levine

Ars Tribunus Militum
1,986
What does the documentation say?
It doesn't.

I believe multimap value insertions abide insert-without-hint semantics, which technically have no defined implementation in pre-C++11 specs. That said, all relevant compilers do an upper bound insert, which should be stable, and I believe C++11 mandates that stability.

(I'm not 100% certain on this; grain of salt. If order is important, I personally think your initial design is better: less ambiguity, and I bet no loss of performance.)
 
levine":1ayrqxst said:
which technically have no defined implementation in pre-C++11 specs. That said, all relevant compilers do an upper bound insert, which should be stable, and I believe C++11 mandates that stability.
Ah. I did my fact checking with the C++11 specs, so you may be right that C++03 doesn't have the same guarantee. But C++11 does indeed mandate this:

23.2.4.4 in n3290":1ayrqxst said:
For multiset and multimap, insert, emplace, and erase preserve the relative ordering of equivalent elements.
Table 102":1ayrqxst said:
If a range containing elements equivalent to t exists in a_eq, t is inserted at the end of that range.
 

Jonathon

Ars Legatus Legionis
17,646
Subscriptor
bames53":28ded1a9 said:
levine":28ded1a9 said:
which technically have no defined implementation in pre-C++11 specs. That said, all relevant compilers do an upper bound insert, which should be stable, and I believe C++11 mandates that stability.
Ah. I did my fact checking with the C++11 specs, so you may be right that C++03 doesn't have the same guarantee. But C++11 does indeed mandate this:

23.2.4.4 in n3290":28ded1a9 said:
For multiset and multimap, insert, emplace, and erase preserve the relative ordering of equivalent elements.
Table 102":28ded1a9 said:
If a range containing elements equivalent to t exists in a_eq, t is inserted at the end of that range.
You're correct; those requirements are not in the C++03 standard (as are lots of others; the containers library spec apparently got a lot of work for C++11... it's a lot less vague now on a lot of points).
 
Status
Not open for further replies.