如何遍历字符串中的单词? - How do I iterate over the words of a string?

问题:

I'm trying to iterate over the words of a string. 我正在尝试遍历字符串中的单词。 ios

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. 请注意,我对C字符串函数或那种字符操做/访问不感兴趣。 Also, please give precedence to elegance over efficiency in your answer. 另外,在回答问题时,请优先考虑优雅而不是效率。 this

The best solution I have right now is: 我目前拥有的最佳解决方案是: spa

#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);
}

Is there a more elegant way to do this? 有没有更优雅的方法能够作到这一点? .net


解决方案:

参考一: https://stackoom.com/question/zQX/如何遍历字符串中的单词
参考二: https://oldbug.net/q/zQX/How-do-I-iterate-over-the-words-of-a-string
相关文章
相关标签/搜索