I have written a small C++ program to keep a count of the alphabets. I am using stl map for the same,
Interestingly, i am not getting the list as it appeared in the input. For example for the word TESTER, my program should give
T 2
E 2
S 1
R 1
But its giving,
E 2
R 1
S 1
T 2
change in the position of the alphabets,
I want the o/p of the alphabets as it appeared in the input. Please help me if i am missing anything.Here is my code
#include<iostream>
#include<map>
using namespace std;
int main()
{
char *str = "TESTER";
map<char,int> checkmap;
map<char,int>::iterator p;
int i;
while( *str != '\0' )
{
p = checkmap.find(*str);
i = p->second;
if(p == checkmap.end())
{
checkmap.insert(std::make_pair(*str,++i));
}
else
{
p->second = ++(p->second);
}
str++;
}
for(p=checkmap.begin(); p!=checkmap.end(); p++)
{
/*if(p->second == 1)
{
cout<<(*p).first<<endl;
}*/
cout<<p->first<<"\t"<<p->second<<endl;
}
return 0;
}