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/

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 JerryCheng 的頭像
    JerryCheng

    KwCheng's blog

    JerryCheng 發表在 痞客邦 留言(0) 人氣()