Is it assured that the elelements in map are ordered

B

baltasarq

Hi !

This is a very specific question, regarding of course standard C++.

Say you have a map: map<int, string> m;, for example.
The main idea/use is that you can look for an int, and get the other
part of the pair.

But you can also run over it with a simple loop, using an interator.

iterator map<int, string>::iterator it = m.begin();
whie( it != end ) {
cout << it->second << endl;
++it;
}

The very specific question is:
Will these elements be printed in order, in STANDARD C++, i.e. in
every possible implementation of C++, or maybe -which is probably more
possible- the internal storing of the elemens in the map is compiler-
specific?

Regards,

Baltasar
 
P

peter koch

Hi !

This is a very specific question, regarding of course standard C++.

Say you have a map: map<int, string> m;, for example.
The main idea/use is that you can look for an int, and get the other
part of the pair.

But you can also run over it with a simple loop, using an interator.

iterator map<int, string>::iterator it = m.begin();
whie( it != end ) {
  cout << it->second << endl;
  ++it;

}

The very specific question is:
Will these elements be printed in order, in STANDARD C++, i.e. in
every possible implementation of C++, or maybe -which is probably more
possible- the internal storing of the elemens in the map is compiler-
specific?
It will printed in order.

/Peter
 
R

Rolf Magnus

baltasarq said:
Hi !

This is a very specific question, regarding of course standard C++.

Say you have a map: map<int, string> m;, for example.
The main idea/use is that you can look for an int, and get the other
part of the pair.

But you can also run over it with a simple loop, using an interator.

iterator map<int, string>::iterator it = m.begin();
whie( it != end ) {
cout << it->second << endl;
++it;
}

The very specific question is:
Will these elements be printed in order, in STANDARD C++, i.e. in
every possible implementation of C++, or maybe -which is probably more
possible- the internal storing of the elemens in the map is compiler-
specific?

The order is specific to the comparison function you use. If you don't
specify it explicitly, that's std::less, with which the map stores the
elements in ascending order.
 

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
474,166
Messages
2,570,901
Members
47,442
Latest member
KevinLocki

Latest Threads

Top