与 stdio 同步是否使程序 I/O 非交互式?
Syncing with stdio is making program i/o non - interactive?
#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。
我无法理解这种行为。
默认情况下,cin
与cout
相关联,因此对这些流的操作是按照它们在程序中写入的顺序进行的。
但是,执行cin.tie(0)
将解开cin
与cout
的束缚,因此可能会穿插cout
和cin
的操作。
请注意,任何一个流上的所有操作仍将按照它们在程序中写入的顺序进行。
相关文章:
- Qt VTK交互风格的信号到小部件
- 如何确定我已使用非编码文件到达 EOF?
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 将公共但非静态的成员函数与ALGLIB集成
- 不同翻译单元中不可重载的非内联函数定义
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 初始化具有非默认构造函数的std::数组项的更好方法
- constexpr 函数中的非文字(通过 std::is_constant_evaluated)
- 如何在C++中使用非静态成员函数作为回调函数
- 数组中最大的非重复元素
- QML:修改在不同QML文件(而非main.QML)中定义的子对象的属性
- 有没有可能有一个只有ADL才能找到的非好友功能
- 与 stdio 同步是否使程序 I/O 非交互式?
- 调用' cdb.exe ' (windbg)以非交互方式运行,并在崩溃时生成回溯
- 简单的c++程序无法作为计划任务运行(交互式/非交互式问题?)
- 构造函数委托如何与非静态数据成员初始化交互
- 非局部静态对象在不同翻译单元中交互的c++示例
- 在gui应用程序中进行交互式控制台/窗口交互