![](/img/trans.png)
[英]can someone explain to me why my string is not not showing in output it happens to me a lot and this is a simple example,
[英]Can someone explain to me why my output is this? And how would I correct my output?
在這段代碼中,我得到的輸出
bbb 55 66 77 88
aaa
我期望和想要的輸出是
bbb 55 66 77 88
bbb
因為我將ss
從log[0]
重新分配給log[1]
。 所以我的問題是,為什么輸出與我期望的不同?如何將其更改為我想要的?
int w,x,y,z;
stringstream ss (stringstream::in | stringstream::out);
string word;
string log[2];
log[0]="aaa 11 22 33 44";
log[1]="bbb 55 66 77 88";
ss<<log[0];
ss>>word;
int k=0;
ss>>w>>x>>y>>z;
k++;
ss<<log[k];
cout<<log[k]<<endl;
ss>>word;
cout<<word<<endl;
return 0;
什么時候
ss >> w >> x >> y >> z;
被執行后,沒有內容可操作,並且ss.good()返回false,這表示需要調用ss.clear()來確保一切正常,我們可以繼續。
但是,鑒於您確實
ss.clear();
ss<<log[k];
ss的內容將是
aaa 11 22 33 44bbb 55 66 77 88
如果您要這樣,可以。 但也許您希望它包含
bbb 55 66 77 88
這是由
ss.str(log[k]);
ss.clear();
當您第二次寫入字符串流時,輸入指針(tellg)設置為-1,並且失敗位被設置。 這意味着第二次寫入和第二次讀取都不會成功。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.