std::ios_base::sync_with_stdio()
来源:互联网 发布:mac找不到硬盘 编辑:程序博客网 时间:2024/06/08 09:14
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';
得到的输出却是
impossibleimpossibleimpossibleCase Number 1: Case Number 2: Case Number 3:
后来仔细地看了看,才发现问题是出在ios_base::sync_with_stdio()这个函数上。
这个函数当我们填入false的值时,相当于把C和C++的输入输出流解绑了,那么cout就能够拥有自己的缓冲区。如果我们去掉解绑的话,那么我们就会看到结果变得和我们预想的是一样了。
Case Number 1: impossibleCase Number 2: impossibleCase 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)
0 0
- std::ios_base::sync_with_stdio()
- ios_base::sync_with_stdio
- ios_base::sync_with_stdio() 的具体用法
- ios_base::sync_with_stdio() 的具体用法
- std::ios_base::ios_base(const std::ios_base&)' is private 错误
- error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
- error: std::ios_base::ios_base(const std::ios_base&)’是私有的
- std::ios_base::ios_base(const std::ios_base&)' is private 错误
- std::ios::sync_with_stdio(false);
- std::ios::sync_with_stdio(false);
- std:ios::sync_with_stdio(false);
- 关于std::ios::sync_with_stdio(false);
- 关于c++文件时error: std::ios_base::ios_base(const std::ios_base&)’是私有的错误
- undefined reference to `std::ios_base::Init::Init()
- undefined reference to `std::ios_base::Init::Init()
- “std::ios_base::Init::Init()”,referenced from 错误
- 读入外挂 std::ios::sync_with_stdio(false);
- std::ios::sync_with_stdio(false); cin.tie(0);
- Android Studio Gradle更新
- 6-超链接
- 编写一个程序,可以一直接收键盘字符,如果是小写字符就输出对应的大写字符,如果接收的是大写字符,就输出对应的小写字符,如果是数字不输出。
- CVI使用数据库时,在未装CVI的计算机上运行错误。
- [LeetCode]2. Add Two Numbers
- std::ios_base::sync_with_stdio()
- android、java制作sdk以及自动生成文档
- php json_encode汉字转义问题
- Sublime Text 3 适用于ionic开发的插件下载
- 一些关于css div的注意事项
- OHEM-Training Region-based Object Detectors with Online Hard Example Mining - cvpr 2016 oral
- Android第一行代码学习笔记八----网络技术
- 使用ButterKnife8.4.0的gradle配置
- C# XMLDocument解析XML归纳