C++ string分割
LDK Lv4

string分割

方法一:使用find()substr()/迭代器.

用字符分割字符串:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 使用字符分割
void Stringsplit(const string& str, const char split, vector<string>& res)
{
if (str == ""){
return
};
//在字符串末尾也加入分隔符,方便截取最后一段
string strs = str + split;
size_t pos = strs.find(split);

// 若找不到内容则字符串搜索函数返回 npos
while (pos != strs.npos)
{
string temp = strs.substr(0, pos);
res.push_back(temp);
//去掉已分割的字符串,在剩下的字符串中进行分割
strs = strs.substr(pos + 1, strs.size());
pos = strs.find(split);
}
}

或者

1
2
3
4
5
6
7
8
9
10
11
12
void stringSplit(const std::string &str, const char sep, std::vector<std::string>& res) {
std::string::const_iterator cur = str.begin();
std::string::const_iterator end = str.end();
std::string::const_iterator next = find(cur, end, sep);
while (next != end) {
res.emplace_back(cur, next);
cur = next + 1;
next = std::find(cur, end, sep);
}
res.emplace_back(cur, next); // 此时next一定等于end
return;
}
用字符串分割字符串

整个字符串splits作为分隔符。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 使用字符串分割
void Stringsplit(const string& str, const string& splits, vector<string>& res)
{
if (str == "") return;
//在字符串末尾也加入分隔符,方便截取最后一段
string strs = str + splits;
size_t pos = strs.find(splits);
int step = splits.size();

// 若找不到内容则字符串搜索函数返回 npos
while (pos != strs.npos)
{
string temp = strs.substr(0, pos);
res.push_back(temp);
//去掉已分割的字符串,在剩下的字符串中进行分割
strs = strs.substr(pos + step, strs.size());
pos = strs.find(splits);
}
}

方法二:使用istringstream.

优点:代码更简洁。缺点:引入了字符串流,性能低于前两种

由 Hexo 驱动 & 主题 Keep
本站由 提供部署服务
总字数 74.8k 访客数 访问量