[問題] strtok v.s getline 切割

看板C_and_CPP作者 (Justin)時間12年前 (2011/12/08 22:15), 編輯推噓0(005)
留言5則, 3人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) c++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 我想請問一下如果只是想針對一行座標去做切割 要用strtok 還是getline比較快? 餵入的資料(Input): (2,3,1)(2,4,1)(4,4,1)(4,3,1) 預期的正確結果(Expected Output): (int)2 3 1 2 4 1 4 4 1 4 3 1 錯誤結果(Wrong Output): 程式碼(Code):(請善用置底文網頁, 記得排版) getline(file,str); stringstream ss(str); string sub_str; while(getline(ss,sub_str,',')) { //cout <<"initial="<< sub_str << endl; stringstream ss2(sub_str); string sub_str2; while(getline(ss2,sub_str2,'(')) { stringstream ss3(sub_str2); string sub_str3; while(getline(ss3,sub_str3,')')) { //cout << sub_str3 << endl; if(sub_str3!=" ") { int i=atoi(sub_str3.c_str()); //cout<<"i="<<i<<endl; } } } } 補充說明(Supplement): 感覺上用getline內建的指令要切割三次,是否改成strtok比較快? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.116.216.118 ※ 編輯: justin719 來自: 140.116.216.118 (12/08 22:15)

12/08 22:16, , 1F
速度的問題,您應該可以自己實際測試並測量吧?
12/08 22:16, 1F

12/08 22:17, , 2F
strtok 好像不能吃string 這樣好像還需要多轉一次char*?
12/08 22:17, 2F

12/08 22:18, , 3F
請你測試看看吧,效能真的是要測過才知道的
12/08 22:18, 3F

12/08 22:19, , 4F
恩 我去試試看 謝謝^^
12/08 22:19, 4F

12/09 16:28, , 5F
12/09 16:28, 5F
文章代碼(AID): #1EuCP_ig (C_and_CPP)