The most elegant way to iterate the words of a string

What is the most elegant way to iterate the words of a string? 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:


For what it’s worth, here’s another way to extract tokens from an input string, relying only on standard library facilities. It’s an example of the power and elegance behind the design of the STL.

Instead of copying the extracted tokens to an output stream, one could insert them into a container, using the same generic copy algorithm.

… or create the vector directly: