#include <string>
#include <vector>
#include <iostream>
#include <string.h>
using namespace std;
void split(const string& src, const string& separator, vector<string>& dest)
{
string str = src;
string substring;
string::size_type start = 0, index;
do
{
index = str.find_first_of(separator,start);
if (index != string::npos)
{
substring = str.substr(start,index-start);
dest.push_back(substring);
start = str.find_first_not_of(separator,index);
if (start == string::npos) return;
}
}while(index != string::npos);
//the last token
substring = str.substr(start);
dest.push_back(substring);
}
int main()
{
vector<string>vStr;
string str="aaa_bbb_ccc_ddd";
split(str,"_",vStr);
cout<<str<<endl;
for (int i=0;i<vStr.size();i++)
{
cout<<vStr[i]<<endl;
}
return 0;
}
C++分割字符串函数封装
最新推荐文章于 2024-07-25 18:17:30 发布