sync_with_stdio(false)的副作用
sync_with_stdio()的一个特性
水一篇随笔
其实对于用快读的大佬来说没什么用,但还是提一下
ios::sync_with_stdio(false)用处是“关闭同步”,从而加快cin与cout的效率。
在部分机子上如果开了这个函数cin和cout跑的还比printf和scanf快。
但是用了sync_with_stdio(false)之后不能与printf和scanf同用,否则会出错。
最近调试的时候发现的:
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cout<<"1\n";
printf("2\n");
cout<<"3\n";
printf("4\n");
}
运行结果是:
2
4
1
3
可以发现开了ios::sync_with_stdio(false)之后,printf函数被提前了,而且这与它在代码中具体出现的位置无关。
至于为什么,据说是C++为了兼容C语言,保证程序在使用std::printf和std::cout的时候不发生混乱(不发生上述情况),将输出流绑到了一起,也就是用了sync_with_stdio()函数:


如果不绑到一起(也就是开个ios::sync_with_stdio(false)),会造成指针读取的混乱,因此输出顺序混乱。
不过在测试的时候我并没有发现cin与scanf的不兼容(出于安全,还是别一起用)。
sync_with_stdio(false)的副作用的更多相关文章
- std::ios::sync_with_stdio(false);
这句语句是用来取消cin的同步,什么叫同步呢?就是iostream的缓冲跟stdio的同步.如果你已经在头文件上用了using namespace std;那么就可以去掉前面的std::了.取消后就c ...
- 关于ios::sync_with_stdio(false)
作用就是取消同步,这样的话使用cin就和使用scanf效率相似. 但是今天在做题的时候碰到一点小问题,就是在关闭同步的时候使用scanf是交了一发代码,然后RE了(经检查scanf没有写错),而把关同 ...
- 关于C++中ios::sync_with_stdio(false)
粘贴自:https://blog.csdn.net/weixin_44015865/article/details/84974373 在C++中的输入和输出有两种方式,一种是scanf和printf, ...
- ios::sync_with_stdio(false)提高C++读写速度
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:ios::sync_with_stdio(false)提高C++读写速度 本文地址:h ...
- 关于std::ios::sync_with_stdio(false)
std::ios::sync_with_stdio(false); 很多C++的初学者可能会被这个问题困扰,经常出现程序无故超时,最终发现问题处在cin和cout上,(甚至有些老oier也会被这个问题 ...
- 关于ios::sync_with_stdio(false);和 cin.tie(0)加速c++输入输出流
原文地址:http://www.hankcs.com/program/cpp/cin-tie-with-sync_with_stdio-acceleration-input-and-output.ht ...
- hdu 1754 I Hate It (线段树、单点更新)(PS:ios::sync_with_stdio(false)可以加快cin、cout的读取写出速度)
I Hate ItTime Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- C++输入输出流加速器,关闭同步流,ios::sync_with_stdio(false)和 cin.tie(0)
leetcode练习时,总会发现运行时间短的代码都会有类似: static int x=[](){ std::ios::sync_with_stdio(false); cin.tie(NULL); ; ...
- C++关闭同步流 ios::sync_with_stdio(false)
说明:ios::sync_with_stdio(false) 1.这句语句是用来取消cin的同步,什么叫同步呢?就是iostream的缓冲跟stdio的同步.这就是为什么cin和cout比scanf和 ...
随机推荐
- GAN的理论 Theory behind GAN
任务:想要找到一个高维空间中的分布 P_data(x),要在目标类别的区域,采样的概率是高的:在那个区域之外,probability是低的.但这个P_data(x)分布的具体形式(pdf)是不知道的, ...
- matlab中set设置图形属性
来源:https://ww2.mathworks.cn/help/matlab/ref/set.html?searchHighlight=set&s_tid=doc_srchtitle set ...
- Java 实现截屏
操作系统:Windows 10 x64 参考:https://blog.csdn.net/weixin_40657079/article/details/83961708 1 import java. ...
- JavaScript查找字符串中给定字符出现的位置以及次数
要求: 给定字符串oabcoefoxyozzopp,要求输出字符o出现的位置和次数. 实现思路: 先查找第一个o出现的位置 然后只要判断indexOf返回的结果,若不是-1,则继续往后查找 因为ind ...
- Swoole实时任务异步调用Demo
server.php <?php class Server { private $serv; private $logFilePath = "/data/wwwroot/houtai/ ...
- 17.JAVA-常用总结
for另一种写法 for(UserBean bean : list){ //for循环取出list中每个成员,并赋给bean变量 System.out.println(bean.getName()); ...
- linux(centos8):prometheus使用alertmanager发送报警邮件(prometheus 2.18.1/alertmanager 0.20.0)
一,alertmanager的用途 1,Alertmanager的作用: Alertmanager是一个独立的报警模块, 它接收Prometheus等客户端发来的警报,并通过分组.删除重复等处理, 通 ...
- linux 压缩 tar命令
linux中tar命令用法 总结 *.tar 用 tar –xvf 解压 *.gz 用 gzip -d或者gunzip 解压 *.tar.gz和*.tgz 用 tar –xzf 解压 *.bz2 ...
- centos8上安装ffmpeg4.2.2并做视频截图
一,ffmpeg的作用: FFmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序. 它提供了录制.转换以及流化音视频的完整解决方案.它包含了非常先进的音频/视频编解码库l ...
- socket php
$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP); socket_bind($socket,'0.0.0.0',6666); while(tru ...