如何遍历由空格分隔的单词组成的字符串的单词?
请注意,我对C字符串函数或那种字符操作/访问不感兴趣。我更喜欢优雅而不是效率。我目前的解决方案:
#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);}