【发布时间】:2020-09-13 18:26:25
【问题描述】:
string ss = "1-1-10-8-9";
上面有一个字符串。 我想在“-”之间的字符串数组中添加元素
字符串向量应与以下相同
vector<string> str = {"1", "1", "10", "8", "9"};
【问题讨论】:
-
这能回答你的问题吗? Splitting a string by a character
string ss = "1-1-10-8-9";
上面有一个字符串。 我想在“-”之间的字符串数组中添加元素
字符串向量应与以下相同
vector<string> str = {"1", "1", "10", "8", "9"};
【问题讨论】:
#include <iostream>
#include <sstream>
#include <vector>
int main() {
std::stringstream ss("1-1-10-8-9");
std::vector<std::string> v;
std::string s;
while(std::getline(ss, s, '-')) {
v.push_back(s);
}
return 0;
}
【讨论】: