How to split a string in C++?

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>
using namespace std;

int main()
{
    string s("Somewhere down the road");
    istringstream iss(s);

    do
    {
        string sub;
        iss >> sub;
        cout << "Substring: " << sub << endl;
    } while (iss);

}
Interestingly this question was asked in verbatim before (spoiler alert):
http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c
Topic archived. No new replies allowed.