等我发现更好的方式再回来。
1.stream流
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
vector<string> split(string s,char token){
stringstream iss(s);
string word;
vector<string> vs;
while(getline(iss,word,token)){
vs.push_back(word);
}
return vs;
}
int main()
{
string s1 = "aaa,sss,ddd,fff";
vector<string> s2 = split(s1, ',');
cout << s1;
for(int i=0;i<s2.size();i++)
{
cout<<s2[i]<<endl;
}
system("pause");
return 0;
}
2.字符串切割
int split_string(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));
return 0;
}