c++给定字符分割

 1 //c++给定字符分割
 2 #include<iostream>
 3 #include<vector>
 4 #include<string.h>
 5 using namespace std;
 6 int main()
 7 {
 8     /* 最终把要分割的字符串根据给定分隔符划分为多个短的字符串*/
 9     vector<string> v;
10     string sa="he**llo,wo,r,ld*aaa";
11     char ch[100];
12     strcpy(ch,sa.c_str());
13     char fgf[]="* ,";
14     char *p;
15     p=strtok(ch,fgf);
16     while(p)
17     {
18         string zfc=p;
19         v.push_back(zfc);
20         p=strtok(NULL,fgf);
21     }
22     for(int i=0;i<v.size();i++)
23     {
24         cout<<v[i]<<endl;
25     }
26 }

但是要注意,给定的分隔符必须是char数组类型。

原文地址:https://www.cnblogs.com/dayq/p/12182086.html