C++ 的字符串分割
vector<string_view> split(const string_view& str,char trim) {
int n = str.size();
vector<string_view> res;
int pos = 0;
while (pos < n) {
while (pos < n && str[pos] == trim) {
pos++;
}
if(pos < n) {
int curr = pos;
while(pos < n && str[pos] != trim) {
pos++;
}
res.emplace_back(str.substr(curr, pos-curr));
}
}
return res;
}
该代码展示了一个C++函数,用于按指定字符分割字符串并返回结果到一个vector<string_view>容器中。它遍历字符串,跳过分隔符,并将每个非分隔符子串添加到结果集合。
168万+

被折叠的 条评论
为什么被折叠?



