simple multiset question

J

John Harrison

If you insert equal values into a multiset is it specified what order they
will have in the multiset. For instance, what should be the output of this
program, is it defined?

#include <set>
#include <iostream>

int main()
{
std::multiset<int> m;
m.insert(1);
std::multiset<int>::iterator i = m.insert(1);
if (i == m.begin())
std::cout << "before\n";
else
std::cout << "after\n";
}

FWIW I tested this on two compilers and got "after" both times but I can't
see this issue addressed in the standard anywhere.

john
 
A

Ali Cehreli

If you insert equal values into a multiset is it specified what order they
will have in the multiset. For instance, what should be the output of this
program, is it defined?

I wasn't able to find that information in the standard as well. My
analyses using g++ had showed that the multimap was preserving the
insertion order.
#include <set>
#include <iostream>

int main()
{
std::multiset<int> m;
m.insert(1);
std::multiset<int>::iterator i = m.insert(1);
if (i == m.begin())
std::cout << "before\n";
else
std::cout << "after\n";
}

FWIW I tested this on two compilers and got "after" both times but I can't
see this issue addressed in the standard anywhere.

There was an earlier thread about this on this newsgroup. Search for a
thread titled "Insertion order of multimap" that was posted on March 11,
2004. The thread doesn't answer your question but still...

Ali
 
J

John Harrison

Ali Cehreli said:
I wasn't able to find that information in the standard as well. My
analyses using g++ had showed that the multimap was preserving the
insertion order.


There was an earlier thread about this on this newsgroup. Search for a
thread titled "Insertion order of multimap" that was posted on March 11,
2004. The thread doesn't answer your question but still...

Ali

Thanks for the reference to the other thread. I guess the answer is that
currently it is implementation defined.

Pete Becker's point 'If you care about the order of things with the same key
then you've made a mistake: they should have different keys.' doesn't apply
to my case since unusually I am using a multimap with mutable keys and I
want to add duplicate values and then modify the keys subsequently. Its
simplifies the code if I can guarantee an order but I guess I can't.

john
 
J

John Harrison

Pete Becker's point 'If you care about the order of things with the same key
then you've made a mistake: they should have different keys.' doesn't apply
to my case since unusually I am using a multimap with mutable keys

I meant multiset.

john
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top