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

Well getline (hence the name) gets a line, not a word.

while( cin >> str ) is perfectly OK if you want the usual white-space semantics for separating words.

So... would you solve the problem the same way as I did?

char ch;
while (in.get(ch)) {
  if ((ch == ' ') || (ch == '\n')) {
    ++stringCounts[str];
    str.erase();
  }
  else {
    str += ch;
  }
}
++stringCounts[str];

I just tried

ifstream in("input.txt")
while (in >> str) ++stringCounts[str] //previously with cin

and it was good enough.

Well, I know that the code posted in P.S. of first post reads file by chars and fills the map ("stringCounts"). This way seems to be better if we have diacritics in file. Right?

This article has been dead for over six months. Start a new discussion instead.