[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Is a std::map<> ordered?

FFMG

11/20/2008 3:22:00 PM

Hi,

If I have something like :

std::map<int, int> myMap;
myMap[200] = 1;
myMap[100] = 2;
myMap[300] = 3;
myMap[150] = 4;

What does the standard say that the output must be if I iterate thru
the map?

for( std::map< int, int>::const_iterator it = myMap.begin(); it !=
myMap.end(); ++it )
{
// output it->first
}

Is the output...
// 100
// 150
// 200
// 300

Or is it
// 200
// 100
// 300
// 150

Or can it be anything at all and I have no real guarantees ...

What I want to do is iterate though the map from the lowest integer to
the largest, do I need to re-order it first or do anything special.

How can I get the map in the right 'order'

Many thanks

FFMG
15 Answers

dirty

4/6/2008 2:07:00 PM

0

On Apr 2, 1:46 am, Tom N <simpleman....@gmail.com> wrote:
> On Apr 1, 11:13 pm, SaveXenu <savex...@live.ca> wrote:
>
> > On Apr 1, 10:07 am, smith.jef...@yahoo.com wrote:> I blew 30 years ago. Last week they called me and started mailing
> > > again.
>
> > Thats funny
>
> It's also bullshit.
>
>
>
> > > If we could all just save one person from Cos, the war will be won.
> > > Dedicate your self to getting just one to blow, using the truth.
>
> >http://www.holysmoke.org/sdhok/c...
> > good reading on the subject matter
>
> Thanks anyway. Paranoid fantasies published by obviously biased
> and dishonest cowards are not what I read when I want to learn
> something about an organization.
>
> I wonder when you are going to finally figure out that most people
> think you are wackjobs?
>
> Since you obviously can't stand it when anyone refuses to accept
> your paranoid fantasies as truth, you must lead very limited social
> lives.
>
> I also wonder when you are going to finally figure out that
> accusations
> that can't be proven aren't worth the paper they are printed on.
>
> If you can't get a prosecutor to file charges, you haven't
> accomplished
> anything. The world is not going to turn on the Church of Scientology
> because a bunch of obvious malicious fanatics without any courage or
> integrity and without any obvious morals at all posts a bunch of
> crap on the Internet and waves signs with cryptic slogans on them
> every few months.
>
> You get media attention for your sensationalistic value, and nothing
> else. The couch potatoes will become bored with you in no time at
> all.
>
> Tom Newton
>
> If I read a post here, I reply to it. If I don't reply to a post, I
> didn't read it.
> my filter kills more than half of the posts here. I don't read from
> google.
>
> I


i like Tom, Tom is awesome. Tom also needs a hug.

Peter Widmer

4/6/2008 2:13:00 PM

0

dirty schrieb:
> On Apr 2, 1:46 am, Tom N <simpleman....@gmail.com> wrote:
>> On Apr 1, 11:13 pm, SaveXenu <savex...@live.ca> wrote:
>>
>>> On Apr 1, 10:07 am, smith.jef...@yahoo.com wrote:> I blew 30 years ago. Last week they called me and started mailing
>>>> again.
>>> Thats funny
>> It's also bullshit.
>>
>>
>>
>>>> If we could all just save one person from Cos, the war will be won.
>>>> Dedicate your self to getting just one to blow, using the truth.
>>> http://www.holysmoke.org/sdhok/c...
>>> good reading on the subject matter
>> Thanks anyway. Paranoid fantasies published by obviously biased
>> and dishonest cowards are not what I read when I want to learn
>> something about an organization.
>>
>> I wonder when you are going to finally figure out that most people
>> think you are wackjobs?
>>
>> Since you obviously can't stand it when anyone refuses to accept
>> your paranoid fantasies as truth, you must lead very limited social
>> lives.
>>
>> I also wonder when you are going to finally figure out that
>> accusations
>> that can't be proven aren't worth the paper they are printed on.
>>
>> If you can't get a prosecutor to file charges, you haven't
>> accomplished
>> anything. The world is not going to turn on the Church of Scientology
>> because a bunch of obvious malicious fanatics without any courage or
>> integrity and without any obvious morals at all posts a bunch of
>> crap on the Internet and waves signs with cryptic slogans on them
>> every few months.
>>
>> You get media attention for your sensationalistic value, and nothing
>> else. The couch potatoes will become bored with you in no time at
>> all.
>>
>> Tom Newton
>>
>> If I read a post here, I reply to it. If I don't reply to a post, I
>> didn't read it.
>> my filter kills more than half of the posts here. I don't read from
>> google.
>>
>> I
>
>
> i like Tom, Tom is awesome. Tom also needs a hug.

Please Tom, configure your filter to kill *all* posts here :-)

Peter

--
Peter Widmer <pwidmer@quicknet.ch>
3803 Beatenberg <http://www.pe...

smith.jeff28

4/6/2008 4:46:00 PM

0

Seems I am not the only one they are tried to recruit after such a
long time.

Any other former members been contacted lately?

Thomas J. Gritzan

11/20/2008 3:27:00 PM

0

FFMG schrieb:
> Hi,
>
> If I have something like :
>
> std::map<int, int> myMap;
> myMap[200] = 1;
> myMap[100] = 2;
> myMap[300] = 3;
> myMap[150] = 4;
>
> What does the standard say that the output must be if I iterate thru
> the map?

A map is always ordered by the key. So the order would be 100, 150, 200,
300 with the corresponding values.

[...]
> What I want to do is iterate though the map from the lowest integer to
> the largest, do I need to re-order it first or do anything special.
>
> How can I get the map in the right 'order'

You can't reorder a map. A std::map (and std::set) is always ordered by
the key, using the given predicate function, which is std::less by default.

--
Thomas

maverik

11/20/2008 3:28:00 PM

0

On Nov 20, 6:21 pm, FFMG <spambuc...@myoddweb.com> wrote:
> What does the standard say that the output must be if I iterate thru
> the map?

According to the Bjarne map values is sorted by keys (17.4.1)

> How can I get the map in the right 'order'

Use sort()

joseph cook

11/20/2008 3:30:00 PM

0

On Nov 20, 10:28 am, maverik <maverik.m...@gmail.com> wrote:
> On Nov 20, 6:21 pm, FFMG <spambuc...@myoddweb.com> wrote:
>
> > What does the standard say that the output must be if I iterate thru
> > the map?
>
>   According to the Bjarne map values is sorted by keys (17.4.1)
>
> > How can I get the map in the right 'order'
>
>   Use sort()

No, this is incorrect. A map is always sorted using std::less on the
key (not the value).

It cannot be reordered.

Joe Cook

maverik

11/20/2008 3:35:00 PM

0

On Nov 20, 6:29 pm, joseph cook <joec...@gmail.com> wrote:
> On Nov 20, 10:28 am, maverik <maverik.m...@gmail.com> wrote:
>
> > On Nov 20, 6:21 pm, FFMG <spambuc...@myoddweb.com> wrote:
>
> > > What does the standard say that the output must be if I iterate thru
> > > the map?
>
> >   According to the Bjarne map values is sorted by keys (17.4.1)
>
> > > How can I get the map in the right 'order'
>
> >   Use sort()
>
> No, this is incorrect.   A map is always sorted using std::less on the
> key (not the value).

Hmmm. I mean map values are sorted by keys. Of course, I mistyped
with "is", but where I said about values?

> It cannot be reordered.

Ya, you are right. When read FFMG's last question I think that he
wants to sort map. Of course, map elements order cannot be changed.

Juha Nieminen

11/20/2008 3:48:00 PM

0

joseph cook wrote:
> A map is always sorted using std::less

Not always. By default, yes, but you can specify other comparators, eg:

std::map<int, int, std::greater> reversedMap;

FFMG

11/20/2008 5:26:00 PM

0


>
> A map is always ordered by the key. So the order would be 100, 150, 200,
> 300 with the corresponding values.
>

Thanks for all the replies.
I just wanted to make sure that the map was always ordered by key.

>
> > How can I get the map in the right 'order'

>
> You can't reorder a map. A std::map (and std::set) is always ordered by
> the key, using the given predicate function, which is std::less by default.
>

What I was trying to say was, 'if the map is not ordered by key how
can I get the map in the right order'.
But seen that it is ordered it does not matter.

> Thomas

Thanks again

FFMG

acehreli

11/20/2008 7:40:00 PM

0

On Nov 20, 7:48 am, Juha Nieminen <nos...@thanks.invalid> wrote:
> joseph cook wrote:
> > A map is always sorted using std::less
>
>   Not always. By default, yes, but you can specify other comparators, eg:
>
> std::map<int, int, std::greater> reversedMap;

Or at runtime:

std::map<int, int> myMap(myPredicate);

Ali