C++ Maps Word Count

Feb 22, 2011 at 7:59pm
Hi,
I think this problem has been asked before but please help me with this specific question.
Word count problem to show the count of each word.

I found this code on the internet:

map<string,int> wordCounter;
string str;

while( cin >> str )
wordCounter[str]++;

The thing is, I don't want to use standard input. I already have a string defined above.. Say str = "Hello Check Hello Hello Check One Two Three"

How can I replace while( cin >> str ) to not use cin but still populate the wordCounter?

Thank you
Feb 22, 2011 at 8:45pm
If you want to use that code with minimal change, create a stringstream from str and use that instead of cin.
http://www.cplusplus.com/reference/iostream/stringstream
Feb 22, 2011 at 8:57pm
Thank you.
Stringstream is what I was looking for :)

Topic archived. No new replies allowed.