Somewhat my code looks like below:
static int myfunc(const string& stringInput)
{
string word;
stringstream ss;
ss << stringInput;
while(ss >> word)
{
++counters[word];
}
...
}
The purpose here is to get an input string (separated by white space ' ') into the string variable word
, but the code here seems to have a lot of overhead -- convert the input string to a string stream and read from the string stream into the target string.
Is there a more elegant way to accomplish the same purpose?
Use stream iterators and a standard function:
If you don't have lambda then:
You are asking how to split a string. Boost has a helpful utility boost::split()
http://www.boost.org/doc/libs/1_48_0/doc/html/string_algo/usage.html#id3115768
Here's an example that puts the resulting words into a vector:
In Visual C++ 11 you can use regex_token_iterator from TR1.
If you concerned about performance (and overheads like string copying), you can write your own routine:
Use ostringstream, maybe