Hey!
Worked on one problem couple days ago and then found code on internet that did almost the same:
map<string,int> stringCounts;
string str;
while( 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;
}
for input:
here are some words and here are some more words
it generates:
word: and, count: 1
word: are, count: 2
word: here, count: 2
word: more, count: 1
word: some, count: 2
word: words, count: 2
I tried to get the "str" from file:
ifstream in("input.txt");
while (getline(in, str)) stringCounts[str]++;
if "input.txt" contains:
here are some words and here are some more words
the output is:
word: here are some words and here are some more words, count: 1
Howcome? Why is it that after we get the string with "cin>>str", "stringCounts[str]++;" fill the map with words, but if we get the string with "getline(in, str)", "stringCounts[str]++;" don't break the string into words?
If any ideas, bring it on!
P.S.: What I did to reach my goal was:
char ch;
while (in.get(ch)) {
if ((ch == ' ') || (ch == '\n')) {
++stringCounts[str];
str.erase();
}
else {
str += ch;
}
}
++stringCounts[str];
But I feel like this is not the best way...
BTW. working in Visual C++ 2008 Express Edition