how to get map datastructure elements in the order of insertion

Hi,
I'm writing a program to find the first no-repeated character in a string. My idea was to scan the string and store the number of occurrence in a map.
This is my code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <map>
#include <string>

using namespace std;

int main()
{
        string candidate = "total";
        map<char,int> chars;

        for(int i=0;i<candidate.length();i++)
        {
                chars[candidate[i]]++;
        }

        map<char, int>::iterator it;
        for(it=chars.begin();it != chars.end(); it++)
        {
                if((*it).second == 1)
                        cout << (*it).first ;
        }
        cout << endl;
        return 0;
}


I expected oal as output, but this is what I got,
alo
.

It seems the map data structure sorts the elements. How can I get the elements in the order I inserted into the map.

Thanks.
You can't std::map will always sort your elements.
In TR1 there is unordered_map which should work but isn't standard http://en.wikipedia.org/wiki/Unordered_map_%28C%2B%2B%29
Topic archived. No new replies allowed.