#include <string.h>
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
//将char数组分割,并返回分割后的数组,split(原始字符串,分隔符,接收数组)
vector<string> split(const string &str,const string &pattern)
{
//const char* convert to char*
char * strc = new char[strlen(str.c_str())+1];
strcpy(strc, str.c_str());
vector<string> resultVec;
char* tmpStr = strtok(strc, pattern.c_str());
while (tmpStr != NULL)
{
resultVec.push_back(string(tmpStr));
tmpStr = strtok(NULL, pattern.c_str());
}
delete[] strc;
return resultVec;
}
int main()
{
char a[100]="a - b - cd - e ";
//split(原始字符串,分隔符,接收数组)
vector<string> svec = split(a,"-");
//输出元素
cout << svec[1] << endl;
for (vector<string>::iterator iter = svec.begin(); iter != svec.end(); ++iter)
{
//输出*iter才是输出那些字符串
cout << *iter << endl;
}
return 0;
}