Home The most elegant way to iterate the words of a string
Reply: 0

The most elegant way to iterate the words of a string

user734
1#
user734 Published in June 25, 2018, 3:56 am

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:

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

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

    do
    {
        string subs;
        iss >> subs;
        cout << "Substring: " << subs << endl;
    } while (iss);
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.417903 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO