#include <iostream>
#include <string>
#include <sstream>
#include <map>
int main()
{
std::string input;
std::cout << "Enter input: ";
std::getline(std::cin, input);
std::map<std::string, int> m;
std::map<std::string, int>::iterator it;
std::istringstream iss(input);
std::string words;
do {
iss >> words;
it = m.find(words);
if(it != m.end())
{
m.insert(it, std::pair<std::string, int>(words, m[words] + 1));
}
else
{
m.insert(std::pair<std::string, int>(words, 1));
}
}while(iss);
for(std::map<std::string, int>::iterator it = m.begin(); it != m.end(); ++it)
{
std::cout << it->first << " - " << it->second << std::endl;
}
return 0;
}
The problem is that it prints 1 for every word, even if it appears twice. What could be the problem? I'm not sure if my test for an iterator not to be empty is correct.