sync_with_stdio

 /*  The synchronization referred to is @e only that between the standard *  C facilities (e.g., stdout) and the standard C++ objects (e.g., *  cout).  User-declared streams are unaffected.  See *  https://gcc.gnu.org/onlinedocs/libstdc++/manual/fstreams.html#std.io.filestreams.binary*/很多人对于同步的问题有误解其实同步只针对cin, cout, cerr, clog(加上宽字符版本)共八个有影响,gcc的实现中是特意为它们定义了stdio_stream_buf的,而且编译器开后门保证它们只初始化一次而我们自己定义流的话,istream. ostream, fstream, sstream都要有一个stream_buf丢进去,这时候,我们根本没法使用stdio_stream_buf能用到的就是file_buf和string_buf, file_buf底层使用的read write等 无缓冲的api,缓冲区是在stream_buf中自己管理的所以自定义的流调用sync_with_stdio是没有意义的:而且要注意sync_with_stdio是一个static成员函数(来自ios_base类),影响是全局性的!
时间: 2024-08-30 06:26:52

sync_with_stdio的相关文章

关于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): 发现与网上的其他高手使用完全相同的方法,使用sca

使用std::ios::tie与std::ios_base::sync_with_stdio加速流

std::ios_base::sync_with_stdio static bool sync_with_stdio( bool sync = true ); 与cstdio流[静态]切换同步 打开或关闭所有的标准iostream流与它们对于的标准C流之间的同步. 实际上,这意味着C++和C流使用相同的缓冲区,因此,可以自由地混合使用流.同步C++标准流可以确保线程安全. 默认情况下,iostream对象和cstdio流同步.如果同步关闭,C++标准流独立地缓冲输入输出,在某些情况下,这是相当快

杭电ACM 找循环节 std::ios::sync_with_stdio(false);

Problem Description As a unicorn, the ability of using magic is the distinguishing feature among other kind of pony. Being familiar with composition and decomposition is the fundamental course for a young unicorn. Twilight Sparkle is interested in th

1, sync_with_stdio(), tie()的应用

一.sync_with_stdio() 这个函数是一个“是否兼容stdio”的开关,C++为了兼容C,保证程序在使用了std::printf和std::cout的时候不发生混乱,将输出流绑在了一起. 在IO之前将stdio接触绑定,可以大大提高IO效率.在操作大数据时,cin,cout的速率也能很快了. 现在,我们通过比较解除绑定前后cin,printf的速率来实际体验下sync_with_stdio()的作用. 首先,我们先产生1000万个随机数作为测试数据.然后,分别用cin,scanf来读

YT14-HDU-找循环节 (关于std::ios::sync_with_stdio(false);的作用和疑问)

Problem Description As a unicorn, the ability of using magic is the distinguishing feature among other kind of pony. Being familiar with composition and decomposition is the fundamental course for a young unicorn. Twilight Sparkle is interested in th

cin.tie与sync_with_stdio加速输入输出

在LeetCode上练习习题的时候每次AC之后都会去看别人的代码,几乎每次都能遇到cin.tie与sync_with_stdio.类似这样: static auto x = [](){ std::ios::sync_with_stdio(false); std::cin.tie(NULL); return 0; }(); class Solution { public: string reverseString(string s) { reverse(s.begin(), s.end()); r

std::ios::sync_with_stdio和tie()——给cin加速

平时在Leetcode上刷题的时候,总能看到有一些题中最快的代码都有这样一段 static const auto init = []() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); return nullptr; }(); 有时候偏偏算法是一样的,但是速度要比没有这段代码的快很多: 查了一下这段代码其实是给cin加速的,也就是说上面提到的题应该是碰到的大数据的输入,而cin cout要比scanf  printf慢很多,很

sync_with_stdio和cin.tie(0); cout.tie(0);

sync_with_stdio 这个函数是一个“是否兼容stdio”的开关,C++为了兼容C,保证程序在使用了std::printf和std::cout的时候不发生混乱,将输出流绑到了一起. 应用 在ACM里,经常出现数据集超大造成 cin TLE的情况.这时候大部分人(包括原来我也是)认为这是cin的效率不及scanf的错,甚至还上升到C语言和C++语言的执行效率层面的无聊争论.其实像上文所说,这只是C++为了兼容而采取的保守措施.我们可以在IO之前将stdio解除绑定,这样做了之后要注意不要

【noip2003】 麦森数

题目描述 形如2P-1的素数称为麦森数,这时P一定也是个素数.但反过来不一定,即如果P是个素数,2P-1不一定也是素数.到1998年底,人们已找到了37个麦森数.最大的一个是P=3021377,它有909526位.麦森数有许多重要应用,它与完全数密切相关. 任务:从文件中输入P(1000<P<3100000),计算2P-1的位数和最后500位数字(用十进制高精度数表示) 输入 文件中只包含一个整数P(1000<P<3100000) 输出 第一行:十进制高精度数2P-1的位数. 第2