[問題] stringstream 清空重複使用

看板C_and_CPP (C/C++)作者 (allen)時間14年前 (2012/02/08 22:16), 編輯推噓2(201)
留言3則, 3人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) VC++ 問題(Question): 如程式中的stringstream為何不能使用呢? stringstream清空再使用需要特殊的方法嗎? (處理flag之類的?) 預期的正確結果(Expected Output): 第三行顯示出"3: 123" 錯誤結果(Wrong Output): 第三行顯示空白 程式碼: #include <iostream> #include <sstream> #include <string> using namespace std; int main() { stringstream ss; string a[5]={""}; int m=123; ss.str(""); ss<<m; ss>>a[1]; //ss.str(""); //加這行也沒用 ss<<m; ss>>a[2]; a[4]="test"; for(int i=0;i<5;i++){ cout<<i<<": "<<a[i]<<endl; } system("pause"); return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.122.197.82

02/08 22:23, , 1F
EOF了
02/08 22:23, 1F

02/08 23:27, , 2F
加一個 ss.clear()
02/08 23:27, 2F

02/13 10:38, , 3F
可以了 謝謝!
02/13 10:38, 3F
文章代碼(AID): #1FCeF42P (C_and_CPP)
文章代碼(AID): #1FCeF42P (C_and_CPP)