<p>How do I iterate over the words of a string composed of words separated by whitespace?</p>
<p>Note that I'm not interested in C string functions or that kind of character manipulation/access. I prefer elegance over efficiency. My current solution:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;string&gt;

using namespace std;

int main() {
    string s = &quot;Somewhere down the road&quot;;
    istringstream iss(s);

    do {
        string subs;
        iss &gt;&gt; subs;
        cout &lt;&lt; &quot;Substring: &quot; &lt;&lt; subs &lt;&lt; endl;
    } while (iss);
}
</code></pre>

The STL does not have such a method available already.
However, you can either use C's
```
strtok()
```
function by using the
```
std::string::c_str()
```
member, or you can write your own. Here is a code sample I found after a quick Google search (
"STL string split"
):
```
```
void Tokenize(const string& str,
              vector<string>& tokens,
              const string& delimiters = " ")
{
    // Skip delimiters at beginning.
    string::size_type lastPos = str.find_first_not_of(delimiters, 0);
    // Find first "non-delimiter".
    string::size_type pos     = str.find_first_of(delimiters, lastPos);

    while (string::npos != pos || string::npos != lastPos)
    {
        // Found a token, add it to the vector.
        tokens.push_back(str.substr(lastPos, pos - lastPos));
        // Skip delimiters.  Note the "not_of"
        lastPos = str.find_first_not_of(delimiters, pos);
        // Find next "non-delimiter"
        pos = str.find_first_of(delimiters, lastPos);
    }
}
```
```
Taken from:
http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html
If you have questions about the code sample, leave a comment and I will explain.
And just because it does not implement a
```
typedef
```
called iterator or overload the
```
<<
```
operator does not mean it is bad code. I use C functions quite frequently. For example,
```
printf
```
and
```
scanf
```
both are faster than
```
std::cin
```
and
```
std::cout
```
(significantly), the
```
fopen
```
syntax is a lot more friendly for binary types, and they also tend to produce smaller EXEs.
Don't get sold on this
"Elegance over performance"
deal.