版权声明:若无来源注明,Techie亮博客文章均为原创。 转载请以链接形式标明本文标题和地址:
本文标题:ios::sync_with_stdio(false)提高C++读写速度     本文地址:http://techieliang.com/2017/11/275/

C++为了兼容C,默认使iostream与stdio关联,使cin与scanf、cout和printf保持同步,保证混用过程中文件指针不混乱。
此方式会造成性能损失,导致使用cin/cout效率远远低于使用scanf/printf。若保证程序只是用一套指令,可取消同步:

  1. std::ios::sync_with_stdio(false);

取消后使用cin/cout速度有明显提升,但注意不要混用iostream与stdio

  1. std::cin.tie(0);

C++98:In terms of static initialization order,cin is guaranteed to be properly constructed and initialized no later than the first time an object of typeios_base::Init is constructed.

C++11:In terms of static initialization order,cin is guaranteed to be properly constructed and initialized no later than the first time an object of typeios_base::Init is constructed, with the inclusion of<iostream> counting as at least one initialization of such objects with static duration.

cin is tied to the standard output stream cout (seeios::tie), which indicates thatcout‘s buffer is flushed (seeostream::flush) before each i/o operation performed oncin.

(来源:http://www.cplusplus.com/reference/iostream/cin/ 网址)

当传入参数为0时,实现cin与cout独立运行,不会等待,提高cin速度

ios::sync_with_stdio(false)提高C++读写速度的更多相关文章

  1. 关于C++中ios::sync_with_stdio(false)

    粘贴自:https://blog.csdn.net/weixin_44015865/article/details/84974373 在C++中的输入和输出有两种方式,一种是scanf和printf, ...

  2. std:ios:sync_with_stdio (false)以及局限性

    如何在输入输出上提高一下效率emmmm #include<iostream> #include<stdio.h> #include<stdlib.h> #inclu ...

  3. 关于ios::sync_with_stdio(false)

    作用就是取消同步,这样的话使用cin就和使用scanf效率相似. 但是今天在做题的时候碰到一点小问题,就是在关闭同步的时候使用scanf是交了一发代码,然后RE了(经检查scanf没有写错),而把关同 ...

  4. 关于std::ios::sync_with_stdio(false)

    std::ios::sync_with_stdio(false); 很多C++的初学者可能会被这个问题困扰,经常出现程序无故超时,最终发现问题处在cin和cout上,(甚至有些老oier也会被这个问题 ...

  5. 关于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 ...

  6. 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 ...

  7. C++输入输出流加速器,关闭同步流,ios::sync_with_stdio(false)和 cin.tie(0)

    leetcode练习时,总会发现运行时间短的代码都会有类似: static int x=[](){ std::ios::sync_with_stdio(false); cin.tie(NULL); ; ...

  8. C++关闭同步流 ios::sync_with_stdio(false)

    说明:ios::sync_with_stdio(false) 1.这句语句是用来取消cin的同步,什么叫同步呢?就是iostream的缓冲跟stdio的同步.这就是为什么cin和cout比scanf和 ...

  9. std::ios::sync_with_stdio(false);

    这句语句是用来取消cin的同步,什么叫同步呢?就是iostream的缓冲跟stdio的同步.如果你已经在头文件上用了using namespace std;那么就可以去掉前面的std::了.取消后就c ...

随机推荐

  1. Python的scrapy之爬取顶点小说网的所有小说

    闲来无事用Python的scrapy框架练练手,爬取顶点小说网的所有小说的详细信息. 看一下网页的构造: tr标签里面的 td 使我们所要爬取的信息 下面是我们要爬取的二级页面 小说的简介信息: 下面 ...

  2. Python学习手册之字符类和元字符深入

    在上一篇文章中,我们介绍了 Python 的正则表达式和元字符,现在我们介绍 Python 的字符类和对元字符进行深入讲解.查看上一篇文章请点击:https://www.cnblogs.com/dus ...

  3. Facebook 被指收集用户数据:通过照片和文本

    北京时间5月25日消息,在加利福尼亚州进行的对Facebook泄露用户信息一案中,法院对Facebook提起一项新的诉讼,指控该公司通过App收集了用户及他们朋友的信息. 上周向加利福尼亚州圣马特奥市 ...

  4. 使用bison和yacc制作脚本语言(3)

    我们现在已经可以写好文法了,下一步我们打算开始正式创建工程了 在工程目录下,我们创建如下文件夹 ./include ./memory ./ms include文件夹下我们将放头文件 memory是内存 ...

  5. STM32 USB设备描述符、配置描述符、端点描述符含义

    查了一整天的资料,自己把不懂的全部试了一遍 一下是程序以及注释 /* USB设备描述符*/ const uint8_t CustomHID_DeviceDescriptor[CUSTOMHID_SIZ ...

  6. C#使用API屏蔽系统热键和任务管理器

    最近做的一个winform类型的项目中需要屏蔽系统热键,在网上搜索了一下,基本上都是调用api来进行hook操作,下面的代码就可以完成功能 using System; using System.IO; ...

  7. AOSP 设置编译输出目录

    export OUT_DIR=/media/caoxinyu/TomasYu/out 注意:export OUT_DIR= OUT_DIR 后面直接跟= ,不要有空格.否则报错.

  8. 使用union 外加count

    explain extended and name='aaa')) t; +----+--------------+------------+-------+---------------+----- ...

  9. C# Builder

    如下: class Program { static void Main(string[] args) { ).BuildB(2.1).BuildUp(); Console.Read(); } } p ...

  10. Visual Studio 2015 Test Explorer does not show anything

    Problem After install Visual Studio 2015 community and NUnit Test Adapter, I cannot find test cases ...