What's the most elegant way to split a string in C++? The string can be assumed to be composed of words separated by whitespace.
(Note that I'm not interested in C string functions or that kind of character manipulation/access. Also, please give precedence to elegance over efficiency in your answer.)
The best solution I have right now is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <sstream>
#include <string>
usingnamespace std;
int main()
{
string s("Somewhere down the road");
istringstream iss(s);
do
{
string sub;
iss >> sub;
cout << "Substring: " << sub << endl;
} while (iss);
}