3

I am trying to make a map of string and integer like this

map<string,int>m;

Insertion is going something like

m["sunday"]=52;
m["monday"]=51;
...
...
...

But whenever i try to iterate it using iterator like this

 map<string,int>::iterator it;

 for(it=m.begin();it!=m.end;it++)
 cout<<it->first<<" "<<it->second;

The output is not like the input inserted.I want to know how to maintain the insertion order in c++ .I searched the web and i found same question but on java.

chota bheem
  • 371
  • 3
  • 8
  • 20
  • Check this post http://stackoverflow.com/questions/19842035/stdmap-how-to-sort-by-value-then-by-key – amahfouz Mar 04 '15 at 21:54
  • I cant use boost.And my task is quite simply that's why i thought that making map and keeping track would be quite hard to implement.Is this the only way.Not something like LinkedHashMaps – chota bheem Mar 04 '15 at 21:55
  • @amahfouz i can't sort them.It will change the question's demand – chota bheem Mar 04 '15 at 22:00
  • 3
    If you can't use boost or sort, then perhaps a `vector< pair< string, int> >` would be more appropriate. – twalberg Mar 04 '15 at 22:05
  • Not really getting it..maybe you would like to expand it sir, – chota bheem Mar 04 '15 at 22:12
  • @twalberg: I know this question is way old... but +1 for a suggestion that uses existing capabilities to simply accomplish the purpose. – guidotex Jul 09 '20 at 15:51

0 Answers0