is STL multimap "find" order stable?

S

salem.ganzhorn

#include <map>
#include <iostream>

using namespace std;

int main(
int argc,
const char * argv[]
)
{
if( 4 != argc ) {
cerr << "usage: " << endl
<< argv[0] << " index value1 value2" << endl;
exit(1);
}
int index = atoi(argv[1]);
int value1 = atoi(argv[2]);
int value2 = atoi(argv[3]);
multimap<int,int> mm;
mm.insert( pair<int,int>(index,value1) );
mm.insert( pair<int,int>(index,value2) );
cout << "Am I guaranteed that " << mm.find(index)->second << " == "
<< value1 << "?" << endl;
return 0;
}

I have read the STL docs and I did not see this explicitly stated.
 
S

salem.ganzhorn

Arg... that is very unfortunate. I might actually have to write some
code :(
Thanks for the info.
 
H

Howard Hinnant

Arg... that is very unfortunate. I might actually have to write some
code :(
Thanks for the info.

Fwiw, I'm trying to change this, at least partly:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1780.html

If accepted, you would have to use lower_bound instead of find, but
otherwise you would be good to go. But as James said, as it stands
today you're just out of luck as far as the standard goes.

However the above paper contains a survey of several libraries (actually
all of them (current versions) that I'm aware of), and they all
implement "insert without hint" as if they were told to "insert at
upper_bound". So from a practical standpoint you might get away with
use of lower_bound instead of find today.

-Howard
 
S

salem.ganzhorn

Howard, thanks so much!
This paper does all the hard work. It is sufficient for me to know that
current implementations are stable when inserting without hints. I will
just add additional unit tests to be sure it doesn't bite me when
someone changes the implementation.
Best wishes,
Salem
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top