Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

std::multimap insertion order guarantees

2 views
Skip to first unread message

Tanguy Fautré

unread,
Oct 5, 2003, 4:40:52 PM10/5/03
to
Hello,

does std::multimap make any guarantee about the insertion order?

for example:

int main()
{
std::multimap<int, int> Map;

Map.insert(std::make_pair(0, 1));
Map.insert(std::make_pair(0, 2));
Map.insert(std::make_pair(0, 3));
Map.insert(std::make_pair(0, 4));

std::copy(Map.begin(), Map.end(), std::ostream_iterator<int>(std::cout,
" "));
}


Will it always output 1 2 3 4 in that order? and will equal_range(0) always
give 1 2 3 4 too?
Or is it platform dependent and is allowed to output 2 4 3 1 for example?

Thanks for your help.
Best regards,

Tanguy


WW

unread,
Oct 5, 2003, 4:48:16 PM10/5/03
to
Tanguy Fautré wrote:
> Hello,
>
> does std::multimap make any guarantee about the insertion order?

Not as far as I know.

--
WW aka Attila


Mike Wahler

unread,
Oct 5, 2003, 5:05:49 PM10/5/03
to
"Tanguy Fautré" <tfa...@pandora.be> wrote in message
news:ol%fb.60237$Kg1.2...@phobos.telenet-ops.be...

> Hello,
>
> does std::multimap make any guarantee about the insertion order?

std::map and std::multimap guarantee that the dereference
of the iterator values from 'begin()' to 'end()' - 1 yield
the elements in 'key' order. For a 'multimap', the order in which
duplicate values in this sequence are retrieved is not guaranteed.

>
> for example:
>
> int main()
> {
> std::multimap<int, int> Map;
>
> Map.insert(std::make_pair(0, 1));
> Map.insert(std::make_pair(0, 2));
> Map.insert(std::make_pair(0, 3));
> Map.insert(std::make_pair(0, 4));
>
> std::copy(Map.begin(), Map.end(),
std::ostream_iterator<int>(std::cout,
> " "));
> }
>
>
> Will it always output 1 2 3 4 in that order?

If you fix that call to copy, yes. (multimap<int,int>::iterator
deref yields type 'std::pair<int, int>', not 'int')

>and will equal_range(0) always
> give 1 2 3 4 too?

Yes.

> Or is it platform dependent and is allowed to output 2 4 3 1 for example?

No.

-Mike


Tanguy Fautré

unread,
Oct 5, 2003, 5:41:18 PM10/5/03
to

"Mike Wahler" <mkwa...@mkwahler.net> wrote in message
news:NI%fb.3370$gA1....@newsread3.news.pas.earthlink.net...


You got me puzzled there.

You say "For a 'multimap', the order in which duplicate values in this


sequence are retrieved is not guaranteed."

but then you say that it will always output 0 1 0 2 0 3 0 4. (if I fix the
copy ;-) to print both the key and the mapped value).

So, will the the elements with the same key keep the order they were
inserted in or not ?

Best regards,

Tanguy


WW

unread,
Oct 5, 2003, 5:42:57 PM10/5/03
to
Tanguy Fautré wrote:
> So, will the the elements with the same key keep the order they were
> inserted in or not ?

Not.

--
WW aka Attila


Howard Hinnant

unread,
Oct 5, 2003, 5:53:10 PM10/5/03
to
In article <blq029$2fr$1...@phys-news1.kolumbus.fi>,
"WW" <wo...@freemail.hu> wrote:

> Tanguy Fautré wrote:
> > Hello,
> >
> > does std::multimap make any guarantee about the insertion order?
>
> Not as far as I know.

Correct. But some vendors may go beyond the standard and provide
additional guarantees. You may decide to depend upon those guarantees
but you must understand that your code is not portable.

For example Metrowerks CodeWarrior does guarantee that the order will be
1 2 3 4. Later insertions are always placed at the end of the equal
range in this implementation, and it is a behavior we will guarantee for
future releases.

-Howard

WW

unread,
Oct 5, 2003, 6:05:19 PM10/5/03
to
Howard Hinnant wrote:
>>> does std::multimap make any guarantee about the insertion order?
>>
>> Not as far as I know.
>
> Correct. But some vendors may go beyond the standard and provide
> additional guarantees. You may decide to depend upon those guarantees
> but you must understand that your code is not portable.
>
> For example Metrowerks CodeWarrior

Specific implementations are off topic in this newsgroup:

http://www.slack.net/~shiva/welcome.txt

--
WW aka Attila


Howard Hinnant

unread,
Oct 5, 2003, 7:10:13 PM10/5/03
to
In article <blq4io$hhh$1...@phys-news1.kolumbus.fi>,
"WW" <wo...@freemail.hu> wrote:

Oh, sorry about that. I'll must mosey on over to the moderated
newsgroups comp.lang.c++.moderated and comp.std.c++ where such a post
would be allowed, and amazingly enough the signal to noise ratio is
soooo much higher due to the lack of so many self appointed moderators.

-Howard

Attila Feher

unread,
Oct 6, 2003, 12:53:31 AM10/6/03
to

Yes. They have no self-appointed moderator. Wait! The self-appointed
moderators _made_ clcppm... Oh well. No noise there, only D marketing for
the last weeks in every topic. BTW the reason why they can afford a wider
area to cover is because of the lower noise ratio and because people rarely
post useless (implementation specific) information... and if they do, they
get pounded upon.

--
Attila aka WW


David B. Held

unread,
Oct 6, 2003, 4:50:44 AM10/6/03
to
"Attila Feher" <attila...@lmf.ericsson.se> wrote in message
news:blqsen$5tb$1...@newstree.wise.edt.ericsson.se...
> Howard Hinnant wrote:
> > [...]

> > Oh, sorry about that. I'll must mosey on over to the
> > moderated newsgroups comp.lang.c++.moderated
> > and comp.std.c++ where such a post would be
> > allowed, and amazingly enough the signal to noise
> > ratio is soooo much higher due to the lack of so many
> > self appointed moderators.

Looks like another fan, Atilla. This is what I'm talking about.

> Yes. They have no self-appointed moderator. Wait! The
> self-appointed moderators _made_ clcppm... Oh well. No
> noise there, only D marketing for the last weeks in every
> topic.

Waaah. Comparing a language that is purportedly derived
from C++ to C++ itself seems quite topical to me, as well
as to numerous individuals who are respected members
of the C++ community. Perhaps you should send a note to
the moderators insisting that your purist sensibilities be
enforced in c.l.c++.m. Isn't it enough that you get to moderate
this group? Do you really need to moderate c.l.c++.m too??

> BTW the reason why they can afford a wider area to cover
> is because of the lower noise ratio and because people
> rarely post useless (implementation specific) information...
> and if they do, they get pounded upon.

Oh, yes. It's useless to know that I can get a compiler which
provides a certain feature above and beyond standard C++.
Which is why no vendor offers such features. Your power
trips are getting increasingly annoying. You create your
own OT threads with your zealous moderation. You are
single-handedly responsible for 50% of the noise in this
NG. Most of the OT-topic posts are one-timers, like the
dude selling books. You turn it into a big thread by
discussing at length how it is off-topic. I see more posts
about moderation than I do about C++!!

If you really want to prove your point about s/n ratios,
why don't you get together with your moderator buddies
and perform an experiment, like a real computer
scientist.

For one month, don't moderate anything. Just let people
post at will. Count the number of OT posts you find,
and save the subject lines for later inspection. Then,
another month, count the OT posts "with moderation".
Include the moderating messages in the count. Compare.
Only then will you prove that pseudo-moderation has
a provably positive effect on the NG. It would be best
if you picked the months (or weeks) at random, and
didn't say which was which until the end of the experiment.

Dave

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003


Attila Feher

unread,
Oct 6, 2003, 5:28:51 AM10/6/03
to
David B. Held wrote:
[PERSONAL INSULTS SNIPPED]
Why don't you get a life and get off my back?

You start to be really annoying. Having spent less than 3 month in this
newsgroup but you already know everything better than anybody else.
Killfile me if you wish or find yourself another occupation than trolling on
each post of mine.

--
Attila aka WW


tom_usenet

unread,
Oct 6, 2003, 7:57:51 AM10/6/03
to
On Sun, 05 Oct 2003 20:40:52 GMT, "Tanguy Fautré" <tfa...@pandora.be>
wrote:

It is platform dependent. Use std::map<int, std::vector<int> > to
control ordering.

Tom

Gary Labowitz

unread,
Oct 6, 2003, 9:39:14 AM10/6/03
to
"Attila Feher" <attila...@lmf.ericsson.se> wrote in message
news:blrcj0$fue$1...@newstree.wise.edt.ericsson.se...

Looks like it's time for a cold shower and a vote.
I vote for keeping OT posts out of the ng. The idea of "letting them in" to
see what happens is an old one, and results in killing off the ng.
I find the C++ topics here interesting, and the answers useful -- even when
they are wrong and must be corrected.

Why anyone would object to keeping the ng focused on C++ is beyond me.
How long have you been in this business?

Keep it up Atilla, it relieves me of having to jump in when the junk begins
to flow.
--
Gary


David B. Held

unread,
Oct 6, 2003, 12:34:23 PM10/6/03
to
"Gary Labowitz" <glab...@comcast.net> wrote in message
news:dNGdnd4T0Or...@comcast.com...
> [...]

> Looks like it's time for a cold shower and a vote.
> I vote for keeping OT posts out of the ng. The idea of
> "letting them in" to see what happens is an old one, and
> results in killing off the ng.

At what point in time was it tried, and for how long, so I
can go back and count the posts myself?

> I find the C++ topics here interesting, and the answers
> useful -- even when they are wrong and must be corrected.

It's not the C++ topics that are the problem. It's the non-C++
ones, obviously.

> Why anyone would object to keeping the ng focused on
> C++ is beyond me.

I would say that of the OT posts I see that get a "moderation
response", maybe less than half are obviously, unambiguously
OT. When they are, this is usually indicated by several people
responding and redirecting the poster. The rest of the "OT"
posts are almost exclusively "moderated" by Atilla, and many
of them meet with objections from other people (besides me,
even!), which means that perhaps Atilla's OT detector
sensitivity is cranked up a bit too high. I think the fact that he
objects to the way c.l.c++.m is moderated might attest to this
fact as well. I haven't seen one other person complain about
that group. Since the marginally OT posts tend to generate
longer meta-threads, I conjecture that, in fact, no moderation
will result in fewer overall posts, and definitely less noise.

> How long have you been in this business?

I admit that I usually read moderated groups or groups that
don't need to be moderated, and it is precisely because
I could see a lot of noise in the general unmoderated groups.
But I was suprised to find that most of the noise was caused
by insiders, and not outsiders.

> Keep it up Atilla, it relieves me of having to jump in when
> the junk begins to flow.

Like now?

0 new messages