- //c++ 给定字符分割
- #include<iostream>
- #include<vector>
- #include<string.h>
- using namespace std;
- int main()
- {
- /* 最终把要分割的字符串根据给定分隔符划分为多个短的字符串 */
- vector<string> v;
- string sa="he**llo,wo,r,ld*aaa";
- char ch[100];
- strcpy(ch,sa.c_str());
- char fgf[]="* ,";
- char *p;
- p=strtok(ch,fgf);
- while(p)
- {
- string zfc=p;
- v.push_back(zfc);
- p=strtok(NULL,fgf);
- }
- for(int i=0;i<v.size();i++)
- {
- cout<<v[i]<<endl;
- }
- }
但是要注意, 给定的分隔符必须是 char 数组类型.
来源: http://www.bubuko.com/infodetail-3377049.html