C++ 没有 split 函数,得自己写一个
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
vector<string> split(string str, char splitStr) {
for (auto& c : str)
if (c == splitStr) // 将无关字符替换为空格,其实这里可以根据需要改造成别的,比如替换函数 replace 字符串,而不是字符
c = ' ';
stringstream ss(str); // 作为流传入ss,流将会以空格切割字符串
vector<string> res; // 结果集
while (ss >> str) { // str 废物再利用
res.push_back(str);
}
return res;
}
int main()
{
vector<string> res = split("/aa/bb/cc", '/');
for (auto i : res)
cout << i << endl;
return 0;
}
如果能使用 boost 库,可以使用 boost 库的 split 函数切割字符串