相关疑难解决方法(0)

如何迭代字符串的单词?

我正在尝试迭代字符串的单词.

可以假设该字符串由用空格分隔的单词组成.

请注意,我对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);
}
Run Code Online (Sandbox Code Playgroud)

有没有更优雅的方式来做到这一点?

c++ string split

2895
推荐指数
43
解决办法
214万
查看次数

反转字符串中单词的顺序

我有这个string s1 = "My name is X Y Z",我想扭转这些词的顺序s1 = "Z Y X is name My".

我可以使用额外的数组来做到这一点.我认为很难,但有可能在现场(不使用额外的数据结构)并且时间复杂度为O(n)吗?

string algorithm data-structures

68
推荐指数
4
解决办法
19万
查看次数

标签 统计

string ×2

algorithm ×1

c++ ×1

data-structures ×1

split ×1