在网上看到的,感觉很简洁方便,不记得在哪里了。
参数说明:
s———带分割的字符串
v———分割的结果
c———按照什么字符串分割
void SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c)
{
std::string::size_type pos1, pos2;
pos2 = s.find(c);
pos1 = 0;
while (std::string::npos != pos2)
{
v.push_back(s.substr(pos1, pos2 - pos1));
pos1 = pos2 + c.size();
pos2 = s.find(c, pos1);
}
if (pos1 != s.length())
v.push_back(s.substr(pos1));
}
调用:
String s="mean_color:[192.55, 204.894, 203.894, 0]";
vector<string> v;
SplitString(s, v, ", ");
cout << v[0] << endl;
cout << v[1] << endl;
cout << v[2] << endl;
结果如下: