作者appleway (apple)
看板ACMCLUB
標題Re: [問題] 擷取字串
時間01/27/2005 00:12:56 Thu
※ 引述《watershed (臨界點)》之銘言:
: 雖然這個問題有點簡單
: 但我還是想知道c++怎麼擷取字串的
: input: aa bb cc
: 我只會用c擷取字串
: char str[100],*p;
: gets(str);
: p = strtok(str," ");
: while( p != NULL ) {
: ...
: p = strtok(NULL," ");
: }
: 這是我看別人程式碼學來的(c++)
: string word,str;
: getline(cin,str);
: istringstream ent(str);
: while( ent >> word ) { ... }
如果這一行改一下呢?
while( getline(ent, word, ';') )
getline可以設定第三個參數 一個char 截止字元不會讀入
我實驗了一下
[yuwei-chang:~/aska] yuwei% ./a.out new
aa;bb;cc
aa
bb
cc
下面是code
string t;
stringstream ss;
getline( fin_1, t);
cout << t << endl;
ss << t;
while(getline(ss, t, ';'))
cout << t << endl;
: 以上兩個程式碼對input都可以擷取
: 但若是input改成 aa;bb;cc 的話
: 第一個程式只要把判斷的字元改一下就行了
: 但第二個程式就沒辦法
: 我是想問c++裡有什麼函式跟第一個程式功能相同? 怎麼寫?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 211.22.17.106