与 stdio 同步是否使程序 I/O 非交互式?


#include <iostream>
using namespace std;
int main()
{   
ios::sync_with_stdio(0);
cin.tie(0);
cout << "Print two numbers: ";
int x, y;
cin >> x >> y;
cout << x << y;

return 0;
}
Input : 23 24 
Output : Print two numbers: 2324

在控制台上打印">打印两个数字:"行之前,stdin 正在等待 x 和 y,然后在控制台上将整个输出打印为 上面给出。

从上述代码中删除同步行后:

#include <iostream>
using namespace std;
int main()
{   
// ios::sync_with_stdio(0);
// cin.tie(0);
cout << "Print two numbers: ";
int x, y;
cin >> x >> y;
cout << x << y;

return 0;
}

这里首先是打印行">打印两个数字:">在控制台上然后 标准输入流正在等待 X 和 Y。

我无法理解这种行为。

默认情况下,cincout相关联,因此对这些流的操作是按照它们在程序中写入的顺序进行的。

但是,执行cin.tie(0)将解开cincout的束缚,因此可能会穿插coutcin的操作。

请注意,任何一个流上的所有操作仍将按照它们在程序中写入的顺序进行。

最新更新