c++ - 为什么在 sync_with_stdio(false) 之前使用 cin 给下一个 i/p 变量随机值

标签 c++ synchronization stdio

这是一个简单的代码:

#include <iostream>
using namespace std;

int main() {
ios::sync_with_stdio(false);
int t,n;cin>>t;

cin.tie(NULL);


while(t--){

    cin>>n;
    cout<<n<<endl;
}
}

如果我输入为

2
1
2

我按预期得到输出 1 2(换行)。 现在,如果我在 ios::sync_with_stdio(false); 之前使用 cin>>t。然后对于相同的 i/p,o/p 是 0 0(换行)。

AFAIK:ios::sync_with_stdio(false); 停止与 c++c i/p 的同步> o/p,但我在这两种情况下都使用了 cin,为什么它不起作用?

最佳答案

此行为是实现定义的。

If this function(Here it refers to sync_with_stdio) is called after I/O has occurred on the standard stream, the behavior is implementation-defined: implementations range from no effect to destroying the read buffer.

关于c++ - 为什么在 sync_with_stdio(false) 之前使用 cin 给下一个 i/p 变量随机值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54493017/

相关文章:

c++ - Matlab 2017a 无法识别编译器(Error using mex No supported compiler or SDK was found)

c++ - 结构和模板功能

c++ - boost::condition_variable::notify_one() 的并发性

c - 使用两个缓冲区和两个互斥锁的线程同步: C

multithreading - linux上的gcc相当于微软的关键部分是什么?

c - 为什么使用 SDL I/O 函数而不是 stdio 函数?

c - 用C中的argv计数

.net - 改用VS10有什么经验?

C++ 函数未按预期工作

C- 文件输入/输出缓冲区和 setvbuf()