请看代码示例, 复制粘贴,运行即可,使用其他字符做分割,则 用该 字符 替换 strtok 函数 第二个参数 即可
// strings and c-strings
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main ()
{
std::string str ("Please split this sentence into tokens");
char * cstr = new char [str.length()+1];
std::strcpy (cstr, str.c_str());
// cstr now contains a c-string copy of str
char * p = std::strtok (cstr," ");
cout<<p<<endl;
while (p!=0)
{
std::cout << p << '\n';
p = std::strtok(NULL," ");
}
delete[] cstr;
return 0;
}