如果我有一个包含以逗号分隔的数字列表的std :: string,那么解析数字并将它们放在整数数组中的最简单方法是什么?
我不想将其概括为解析其他任何内容.只是一个逗号分隔整数的简单字符串,如"1,1,1,1,2,1,1,1,0".
我试图插入一个由空格分隔的字符串到一个字符串数组,而不使用C++中的vector.例如:
using namespace std;
int main() {
string line = "test one two three.";
string arr[4];
//codes here to put each word in string line into string array arr
for(int i = 0; i < 4; i++) {
cout << arr[i] << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
我希望输出为:
test
one
two
three.
Run Code Online (Sandbox Code Playgroud)
我知道在C++中已经有很多问题要求字符串>数组.我意识到这可能是一个重复的问题,但我找不到任何满足我条件的答案(将字符串拆分为数组而不使用向量).如果这是一个重复的问题,我会提前道歉.
有点我的代码如下所示:
static int myfunc(const string& stringInput)
{
string word;
stringstream ss;
ss << stringInput;
while(ss >> word)
{
++counters[word];
}
...
}
Run Code Online (Sandbox Code Playgroud)
这里的目的是获取一个输入字符串(由空格''分隔)到字符串变量中word
,但这里的代码似乎有很多开销 - 将输入字符串转换为字符串流并从字符串流读取到目标字符串.
是否有更优雅的方式来实现相同的目的?