【问题标题】:Syncing with stdio is making program i/o non - interactive?与 stdio 同步会使程序 i/o 非交互式?
【发布时间】:2020-07-03 18:06:28
【问题描述】:
#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

在控制台、标准输入上打印“打印两个数字:”行之前的此处 正在等待 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;
}

这里首先是在控制台上打印“Print two numbers:”行 标准输入流正在等待 x 和 y。

我无法理解这种行为。

【问题讨论】:

标签: c++ io stl legacy-code


【解决方案1】:

默认情况下,cin 绑定到 cout,因此对这些流的操作按照它们在程序中的写入顺序进行。

但是,cin.tie(0) 会从cout 解开cin,所以coutcin 上的操作可能会穿插。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-14
    相关资源
    最近更新 更多