close
Although stringstream is very convenient to us ,
but sometimes we can also use c method to split the string.
first , we need to convert string to c_string style. (char*)
then , we can use strtok to split this string by specific format.
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main ()
{
char * cstr,
*p; string str ("Please split this phrase into tokens");
cstr = new char [str.size()+1]
;
strcpy (cstr, str.c_str());
// cstr now contains a c-string copy of str
p=strtok (cstr," ");
while (p!=NULL)
{
cout << p << endl;
p=strtok(NULL," ");
}
delete[] cstr;
return 0;
}
ref : http://www.cplusplus.com/reference/string/string/c_str/
全站熱搜