关于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.html
http://www.clanfei.com/2012/03/235.html
在网上查看别人的ACM代码时,发现别人输入输出语句用的总是scanf与printf,有点不解,还以为他们用的都是C语言,而非C++,但今天做的一道题(Sort):
发现与网上的其他高手使用完全相同的方法,使用scanf及printf的代码提交后Accepted,而使用cin及cout的却Time Limit Exceeded,代码如下:
代码一(Accepted):
- #include<iostream>
- using namespace std;
- bool a[1000001];
- int main()
- {
- int n, m, num, count;
- while(scanf("%d%d",&n,&m)!=EOF){
- memset(a, 0, sizeof(a));
- for(int i=0; i<n; i++){
- scanf("%d",&num);
- a[num + 500000] = 1;
- }
- count = 0;
- for(int j = 1000000; j >= 0; --j){
- if(a[j]){
- if(count == m - 1){
- printf("%d\n",j-500000);
- break;
- }
- printf("%d ",j-500000);
- count++;
- }
- }
- }
- return 0;
- }
代码二(Time Limit Exceeded):
- #include<iostream>
- using namespace std;
- bool a[1000001];
- int main()
- {
- int n, m, num, count;
- while(cin >> n >> m){
- memset(a, 0, sizeof(a));
- for(int i=0; i<n; i++){
- cin >> num;
- a[num + 500000] = 1;
- }
- count = 0;
- for(int j = 1000000; j >= 0; --j){
- if(a[j]){
- if(count == m - 1){
- cout << j - 500000 << endl;
- break;
- }
- cout << j - 500000 << " ";
- count++;
- }
- }
- }
- return 0;
- }
可以看出,代码思路完全一样,只是输入输出方法不同,问过老师,加上这一句代码后使用cin及cout也可以Accepted:
- std::ios::sync_with_stdio(false);
百 度了一下,原来而cin,cout之所以效率低,是因为先把要输出的东西存入缓冲区,再输出,导致效率降低,而这段语句可以来打消iostream的输入 输出缓存,可以节省许多时间,使效率与scanf与printf相差无几,还有应注意的是scanf与printf使用的头文件应是stdio.h而不是 iostream。
我是怎么在不知道这一对函数的情况下活到今天的,以前碰到cin TLE的时候总是傻乎乎地改成scanf,甚至还相信过C++在IO方面效率低下的鬼话,殊不知这只是C++为了兼容C而采取的保守措施。
tie
tie是将两个stream绑定的函数,空参数的话返回当前的输出流指针。
- #include <iostream>
- #include <fstream>
- ///////////////////////////SubMain//////////////////////////////////
- int main(int argc, char *argv[])
- {
- std::ostream *prevstr;
- std::ofstream ofs;
- ofs.open("test.txt");
- std::cout << "tie example:\n"; // 直接输出到屏幕
- *std::cin.tie() << "This is inserted into cout\n"; // 空参数调用返回默认的output stream,也就是cout
- prevstr = std::cin.tie(&ofs); // cin绑定ofs,返回原来的output stream
- *std::cin.tie() << "This is inserted into the file\n"; // ofs,输出到文件
- std::cin.tie(prevstr); // 恢复
- ofs.close();
- system("pause");
- return 0;
- }
- ///////////////////////////End Sub//////////////////////////////////
输出:
- tie example:
- This is inserted into cout
- 请按任意键继续. . .
同时当前目录下的test.txt输出:
- This is inserted into the file
sync_with_stdio
这个函数是一个“是否兼容stdio”的开关,C++为了兼容C,保证程序在使用了std::printf和std::cout的时候不发生混乱,将输出流绑到了一起。
应用
在ACM里,经常出现 数据集超大造成 cin TLE的情况。这时候大部分人(包括原来我也是)认为这是cin的效率不及scanf的错,甚至还上升到C语言和C++语言的执行效率层面的无聊争论。其 实像上文所说,这只是C++为了兼容而采取的保守措施。我们可以在IO之前将stdio解除绑定,这样做了之后要注意不要同时混用cout和printf 之类。
在默认的情况下cin绑定的是cout,每次执行 << 操作符的时候都要调用flush,这样会增加IO负担。可以通过tie(0)(0表示NULL)来解除cin与cout的绑定,进一步加快执行效率。
如下所示:
- #include <iostream>
- int main()
- {
- std::ios::sync_with_stdio(false);
- std::cin.tie(0);
- // IO
- }
关于ios::sync_with_stdio(false);和 cin.tie(0)加速c++输入输出流的更多相关文章
- C++输入输出流加速器,关闭同步流,ios::sync_with_stdio(false)和 cin.tie(0)
leetcode练习时,总会发现运行时间短的代码都会有类似: static int x=[](){ std::ios::sync_with_stdio(false); cin.tie(NULL); ; ...
- sync_with_stdio(false)和cin.tie(NULL)
std::ios::sync_with_stdio(false) 这个函数相当于是否兼容stdio的开关,默认为true C++为了兼容C,保证程序在使用了std::printf和std::cout的 ...
- 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 ...
- ios::sync_with_stdio(false)提高C++读写速度
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:ios::sync_with_stdio(false)提高C++读写速度 本文地址:h ...
- 关于C++中ios::sync_with_stdio(false)
粘贴自:https://blog.csdn.net/weixin_44015865/article/details/84974373 在C++中的输入和输出有两种方式,一种是scanf和printf, ...
- 关于ios::sync_with_stdio(false)
作用就是取消同步,这样的话使用cin就和使用scanf效率相似. 但是今天在做题的时候碰到一点小问题,就是在关闭同步的时候使用scanf是交了一发代码,然后RE了(经检查scanf没有写错),而把关同 ...
- 关于std::ios::sync_with_stdio(false)
std::ios::sync_with_stdio(false); 很多C++的初学者可能会被这个问题困扰,经常出现程序无故超时,最终发现问题处在cin和cout上,(甚至有些老oier也会被这个问题 ...
- C++关闭同步流 ios::sync_with_stdio(false)
说明:ios::sync_with_stdio(false) 1.这句语句是用来取消cin的同步,什么叫同步呢?就是iostream的缓冲跟stdio的同步.这就是为什么cin和cout比scanf和 ...
- std:ios:sync_with_stdio (false)以及局限性
如何在输入输出上提高一下效率emmmm #include<iostream> #include<stdio.h> #include<stdlib.h> #inclu ...
随机推荐
- 三星DRAM+NAND FLASH 合成MCP芯片介绍及应用攻略
转自:http://blog.csdn.net/gao5528/article/details/6256119 三星DRAM+NAND FLASH 合成MCP芯片介绍及应用攻略(K5系列产品篇) 一年 ...
- Maven项目Update Project...后JRE System Library会自动变回1.5解决办法
<build> <finalName>pay</finalName> <plugins> <plugin> <groupId>o ...
- 精通 Android Data Binding
转自:https://github.com/LyndonChin/MasteringAndroidDataBinding 官方虽然已经给出了教程 - Data Binding Guide (中文版 - ...
- Warning: (3719, “‘utf8’ is currently an alias for the character set UTF8MB3, which will be replaced by UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.”)
[1]本地版本 Mysql 8.0.12 创建表SQL: DROP TABLE IF EXISTS students; CREATE TABLE `students` ( `sId` ) UNSIGN ...
- [Android Studio 权威教程]AS 中配置强大的版本号管理系统(Git、SVN、等)
在Eclipse中加入Git等版本号管理工具须要自己加入插件.并且个人认为不咋好用,在AS中已经给我们集成好了,我们仅仅须要配置一下就OK了.今天就和大家聊聊怎么配置以及使用的要点. 1. 安装Git ...
- Android 与H5之间的js交互
之前项目做过一些Android和Html5之间js交互方面的东西,今天有时间就总结一下: 一.为什么要进行js交互: 为了方便原生开发和Html之间数据传递,在静态页面的情况下可以改变原生开发的页面: ...
- Idea 远程调试jenkins 项目
1.Jenkins配置 jenkins 服务启动时 需要在jvm启动项里加入如下代码: -Xdebug -Xrunjdwp:transport=dt_socket,suspend=n,server=y ...
- iptables的扩展,layer7实现
l7filter:介绍 能过滤7层协议,例如过滤QQ.迅雷 原版只支持2.6.28,不支持centos6的2.6.32:马哥的版本支持2.6.32 实现l7filter: 给内核(netfilter) ...
- iOS启动页加载广告
1.定义全局成员变量 @interface AppDelegate () @property (strong, nonatomic) UIImageView *adImageView; @proper ...
- CSS 布局实例系列(三)如何实现一个左右宽度固定,中间自适应的三列布局——也聊聊双飞翼
今天聊聊一个经典的布局实例: 实现一个三列布局,其中左侧和右侧的部分宽度固定,中间部分宽度随浏览器宽度的变化而自适应变化 可能很多朋友已经笑了,这玩意儿通过双飞翼布局就能轻松实现.不过,还请容我在双飞 ...