原文地址: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):

  1. #include<iostream>
  2. using namespace std;
  3. bool a[1000001];
  4. int main()
  5. {
  6. int n, m, num, count;
  7. while(scanf("%d%d",&n,&m)!=EOF){
  8. memset(a, 0, sizeof(a));
  9. for(int i=0; i<n; i++){
  10. scanf("%d",&num);
  11. a[num + 500000] = 1;
  12. }
  13. count = 0;
  14. for(int j = 1000000; j >= 0; --j){
  15. if(a[j]){
  16. if(count == m - 1){
  17. printf("%d\n",j-500000);
  18. break;
  19. }
  20. printf("%d ",j-500000);
  21. count++;
  22. }
  23. }
  24. }
  25. return 0;
  26. }

代码二(Time Limit Exceeded):

  1. #include<iostream>
  2. using namespace std;
  3. bool a[1000001];
  4. int main()
  5. {
  6. int n, m, num, count;
  7. while(cin >> n >> m){
  8. memset(a, 0, sizeof(a));
  9. for(int i=0; i<n; i++){
  10. cin >> num;
  11. a[num + 500000] = 1;
  12. }
  13. count = 0;
  14. for(int j = 1000000; j >= 0; --j){
  15. if(a[j]){
  16. if(count == m - 1){
  17. cout << j - 500000 << endl;
  18. break;
  19. }
  20. cout << j - 500000 << " ";
  21. count++;
  22. }
  23. }
  24. }
  25. return 0;
  26. }

可以看出,代码思路完全一样,只是输入输出方法不同,问过老师,加上这一句代码后使用cin及cout也可以Accepted:

  1. 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绑定的函数,空参数的话返回当前的输出流指针。

  1. #include <iostream>
  2. #include <fstream>
  3. ///////////////////////////SubMain//////////////////////////////////
  4. int main(int argc, char *argv[])
  5. {
  6. std::ostream *prevstr;
  7. std::ofstream ofs;
  8. ofs.open("test.txt");
  9. std::cout << "tie example:\n"; // 直接输出到屏幕
  10. *std::cin.tie() << "This is inserted into cout\n"; // 空参数调用返回默认的output stream,也就是cout
  11. prevstr = std::cin.tie(&ofs); // cin绑定ofs,返回原来的output stream
  12. *std::cin.tie() << "This is inserted into the file\n"; // ofs,输出到文件
  13. std::cin.tie(prevstr); // 恢复
  14. ofs.close();
  15. system("pause");
  16. return 0;
  17. }
  18. ///////////////////////////End Sub//////////////////////////////////

输出:

  1. tie example:
  2. This is inserted into cout
  3. 请按任意键继续. . .

同时当前目录下的test.txt输出:

  1. 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的绑定,进一步加快执行效率。

如下所示:

    1. #include <iostream>
    2. int main()
    3. {
    4. std::ios::sync_with_stdio(false);
    5. std::cin.tie(0);
    6. // IO
    7. }

最新文章

  1. STM32F412应用开发笔记之二:基本GPIO控制
  2. ECshop后台角色权限的添加和分配
  3. 【DPM】Deformable Part Models matlab代码在windows下的调试过程
  4. operator
  5. BZOJ-1834 网络扩容 最小费用最大流+最大流+乱搞
  6. (四)动态生成控件,点击button添加控件
  7. 【 D3.js 选择集与数据详解 — 4 】 enter和exit的处理方法以及处理模板
  8. [leetcode-508-Most Frequent Subtree Sum]
  9. Windows Azure系列-- 配置Azure Power Shell
  10. 《前端之路》之四 JavaScript 的闭包、作用域、作用域链
  11. java反射机制的简单使用
  12. Python学习之旅(二十二)
  13. layui(一)——layDate组件常见用法
  14. 8 -- 深入使用Spring -- 6... Spring的事务
  15. 爱今天 APP 闪退怎么办?
  16. NumPy 副本和视图
  17. ansible debug模块学习笔记
  18. printk()函数学习笔记
  19. eclipse调试时增加jvm参数
  20. 高并发系列之——MQ消息中间件Kafka

热门文章

  1. 2018年EI收录中文期刊目录【转】
  2. Ubuntu 16.04下编译安装Apache2.4和PHP7结合
  3. linux 上安装apache 出现 configure: error: APR not found. Please read the documentation错误
  4. eclipse配置python插件
  5. iOS --创建文件夹 ,删除文件夹
  6. Unity 插件收集(持续更新)
  7. Java水印图片处理
  8. 【BZOJ5016】[Snoi2017]一个简单的询问 莫队
  9. 我的Android进阶之旅------>Android无第三方Jar包的源代报错:The current class path entry belongs to container ...的解决方法
  10. FPGA低温不能启动分析