std::ios_base::sync_with_stdio()
2016-09-23 15:56
411 查看
std::ios_base::sync_with_stdio()
今天在做OJ的时候意外地发现printf和cout的输出顺序和代码顺序不一样,如下printf("Case Number %d: ", ++cas); if(takeTime[0][1] >= 0x3f3f3f3f) cout << "impossible\n"; else cout << takeTime[0][1] << '\n';
得到的输出却是
impossible impossible impossible Case Number 1: Case Number 2: Case Number 3:
后来仔细地看了看,才发现问题是出在ios_base::sync_with_stdio()这个函数上。
这个函数当我们填入false的值时,相当于把C和C++的输入输出流解绑了,那么cout就能够拥有自己的缓冲区。如果我们去掉解绑的话,那么我们就会看到结果变得和我们预想的是一样了。
Case Number 1: impossible Case Number 2: impossible Case Number 3: impossible
所以将C++和C的输入输出流同步,是一种比较安全的方式,不要滥用ios_base::sync_with_stdio()
cin.tie()
还有一个值得一提的就是cin.tie().和sync_with_sdtio类似,cin.tie()也是为了让输入输出变得比较安全的方式。当我们填入NULL或者0解绑之后,对于
cout << "Enter name:"; cin >> name;
就会发现,是先输入,再输出’Enter name:’
这是因为解绑了输入输出之后cout需要在缓冲区写入之后才能执行
参考
Significance of ios_base::sync_with_stdio(false); cin.tie(NULL)相关文章推荐
- 转载——探寻C++最快的读取文件的方案std::ios::sync_with_stdio(false)——转载
- std::ios::sync_with_stdio(false); cin.tie(0);
- YT14-HDU-找循环节 (关于std::ios::sync_with_stdio(false);的作用和疑问)
- std::ios::sync_with_stdio(false);
- 探寻C++最快的读取文件的方案std::ios::sync_with_stdio(false) (转)
- 读入外挂 std::ios::sync_with_stdio(false);
- ios_base::sync_with_stdio() 的具体用法
- std::ios::sync_with_stdio(false);--------探寻C++最快的读取文件的方案
- ios_base::sync_with_stdio
- std:ios::sync_with_stdio(false);
- std::ios::sync_with_stdio(false);
- 关于std::ios::sync_with_stdio(false);
- C++ 里利用 std::ios::sync_with_stdio(false) 解决TLE问题
- 杭电ACM 找循环节 std::ios::sync_with_stdio(false);
- ios_base::sync_with_stdio() 的具体用法
- std::ios::sync_with_stdio(false);
- ios_base::sync_with_stdio(false)可以来打消iostream的输入输出缓存,可以节省许多时间,使效率与scanf与printf相差无几
- ios::sync_with_stdio(false);(读入优化)
- ios::sync_with_stdio();
- 关于ios::sync_with_stdio(false);和 cin.tie(0)加速c++输入输出流