Actually your code (or a compiling version of it) works.
#include <iostream>
#include <map>
#include <string>
#include <iterator>
using namespace std;
int main()
{
map<string, int> stringCounts;
string str;
while (getline(cin, str))
stringCounts[str]++;
map<string, int>::iterator iter;
for (iter = stringCounts.begin(); iter != stringCounts.end(); iter++)
{
cout << "word: " << iter->first << ", count: " << iter->second << endl;
}
}
sample input:
as
asas
asas
aa
output
word: aa, count: 1
word: as, count: 1
word: asas, count: 2
[update]
The same program written using modern
C++
:
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<string, int> stringCounts;
string str;
while (getline(cin, str))
stringCounts[str]++;
for (auto p : stringCounts)
cout << "word: " << p.first << ", count: " << p.second << endl;
}
[/update]