如何遍歷字符串中的單詞? - 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
相關文章
相關標籤/搜索