By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,131 Members | 2,321 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,131 IT Pros & Developers. It's quick & easy.

simple multiset question

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Wed, 04 Aug 2004 07:57:18 +0100, John Harrison wrote:
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

Jul 22 '05 #2

P: n/a

"Ali Cehreli" <ac******@yahoo.com> wrote in message
news:pa****************************@yahoo.com...
On Wed, 04 Aug 2004 07:57:18 +0100, John Harrison wrote:
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


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
Jul 22 '05 #3

P: n/a
>
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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.