Qt下 QString 实现了split()函数,而std::string则没有实现,STL中也没有实现,只能自己写一个了。
#include <string>
#include <vector>
using namespace std;
vector<string> split(string strtem,char a)
{
vector<string> strvec;
string::size_type pos1, pos2;
pos2 = strtem.find(a);
pos1 = 0;
while (string::npos != pos2)
{
strvec.push_back(strtem.substr(pos1, pos2 - pos1));
pos1 = pos2 + 1;
pos2 = strtem.find(a, pos1);
}
strvec.push_back(strtem.substr(pos1));
return strvec;
}

本文介绍了一种在C++中实现字符串split功能的方法,通过自定义函数split来处理std::string类型的字符串,使其能够按指定字符进行分割并返回一个包含分割结果的vector容器。
147





