[英]In C++, how is cin and cout objects in relation to its class iostream?
[英]c++ syncing of cin and cout with iostream
在這里需要一些幫助;
look at this code:
#include<bits/stdc++.h>
using namespace std;
int main()
{ ios::sync_with_stdio(false);
cin.tie(false);
while(true)
{
int x;
cin>>x;
cout<<x<<"\n";
if(x==42){
break;
}
}
return 0;
}
現在這個:
#include<bits/stdc++.h>
using namespace std;
int main()
{
while(true)
{
int x;
cin>>x;
cout<<x<<"\n";
if(x==42){
break;
}
}
return 0;
}
當我同時運行這兩個代碼時,第一個代碼首先接受所有輸入,然后顯示輸出; 但是,用相同的代碼,第二個代碼同時讀取並打印出這里的問題,我無法理解。
區別是cin.tie(false);
(實際上應該是cin.tie(nullptr);
)。 它消除了cin
和cout
之間的紐帶。
默認情況下,將對流進行緩沖,因此直到緩沖區已滿或流被刷新(以先發生者為准),才會顯示寫入cout
的文本。
但是,默認情況下, cin
也“綁定”到cout
並在每次輸入之前刷新緩沖區。 這樣就可以在輸入之前看到寫入cout
所有提示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.