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慢很多,很容易就time error了;

这应该也是一些acm dalao用printf scanf而不用cincout的原因吧;

具体是:

  1. C++为了让cin cout和scanf printf 同时使用时不产生混乱,所以c++用一个缓冲区来同步c的标准流,
    而std::ios::sync_with_stdio(false)可以关闭这一个同步,让cin和cout不经过缓冲区;
  2. tie()函数是把两个stream绑定到一起,flush()是把缓冲区的数据输出到文件,而cin和cout是默认绑定在一起的,
    每次使用都会调用flush(), 而cin.tie(nullptr)可以解除这个绑定;
  3. cin和cout默认绑定在一起,是为了防止cin出现在cout之前,在绑定后,
    每次cin都会先把缓冲区的数据刷新到输出文件中

今天也碰到了一个大数据输入的题:

嘛,其实这里主要是算法的问题;

改进之后,这里还没用上述的代码:

使用之后:

还是有效果的;

原文地址:https://www.cnblogs.com/mckc/p/9886378.html

时间: 2024-07-30 16:06:14

std::ios::sync_with_stdio和tie()——给cin加速的相关文章

杭电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

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

关于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集训队第一届暑假友谊赛 B FYZ的求婚之旅 D 计算机科学家 F 智慧码 题解

比赛网址:https://ac.nowcoder.com/acm/contest/994#question B FYZ的求婚之旅 思路: 然后用快速幂即可. 细节见代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack>

2019 杭电多校 第九场

2019 Multi-University Training Contest 9 补题链接:2019 Multi-University Training Contest 9 1005 Rikka with Game (HDU 6684) 题意 Rikka 和 Yuta 玩游戏.给定一个字符串.两人轮流对字符串操作.可以选择结束游戏,也可以改变其中一个字符,改变规则是:\(a\rightarrow b,b\rightarrow c,-,y\rightarrow z,z\rightarrow a.\

Sicily 13460. Passwords

13460. Passwords Constraints Time Limit: 1 secs, Memory Limit: 256 MB Description Mirko is an evil plotting genius and has gotten hold of a list of all possible passwords for a certain user account. The first thing he noticed was all the passwords ar

二分暑假专题 训练记录 2017-7-29

POJ3258-River Hopscotch 题意: 给你区间[0,L]给你n个石头,然后去除m个石头  最大化 石头间最小的距离 思路: 首先0和L 这两个石头是不可以动的   然后用 s 数组记录 整个区间的石头 然后排序  此时石头的排序就是有序的了  然后二分套模板 接着check函数才是最关键的好的把 从0到 n+1-m   总共就有 n+2-m 个石头了 而由于第0个石头不可以动 , 所以从第一个开始动 同时判断条件是 s[cur] - s[last] < d 而不是 <= #i

jxnu acm新生选拔赛

最小的数 Problem Description 定义一种正整数集合K,集合中有N个数,集合中元素Ki(1<=i<=N)是包含i个不同质因子的最小的数.因为Ki可能会很大,所以将集合中所有Ki对10^9+7取余. Input 本题只有唯一一组测试数据,第一行给出N,q,表示K的个数以及q次询问.1<=N<=1000,q<=10^5.接下来q行每行一个正整数(64位整数范围内),请判断其对10^9+7取余后,是否在集合K中. Output 对于每次询问,根据题意输出Yes或No