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

粘贴自:https://blog.csdn.net/weixin_44015865/article/details/84974373

还是不要用了,做ZOJ-4016(URL: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016)还是会超时啊,还是老老实实用scanf和printf吧!

在C++中的输入和输出有两种方式,一种是scanf和printf,另一种是cin和cout,在#include<bits/stdc++.h>这个万能头文件下,这两种方式是可以互换的
C++和C很相似,很多大佬都用C++写C,但是在后面的一种方式中cin和cout的输入和输出效率比第一种低,原来而cin,cout之所以效率低,是因为先把要输出的东西存入缓冲区,再输出,导致效率降低,而这段语句可以来打消iostream的输入 输出缓存,可以节省许多时间,使效率与scanf与printf相差无几,还有应注意的是scanf与printf使用的头文件应是stdio.h而不是 iostream。
在学校的OJ上后面的时间复杂度要求很低,有好多时候TLE不是因为代码的问题,对于初学C++的人来说根本不知道ios::sync_with_stdio(false);这个东西。
以下代码是SDUT上的一个数据结构的题目
Logout
顺序表应用4-2:元素位置互换之逆置算法(数据改进)
Time Limit: 80 ms Memory Limit: 600 KiB

Problem Description
一个长度为len(1<=len<=1000000)的顺序表,数据元素的类型为整型,将该表分成两半,前一半有m个元素,后一半有len-m个元素(1<=m<=len),设计一个时间复杂度为O(N)、空间复杂度为O(1)的算法,改变原来的顺序表,把顺序表中原来在前的m个元素放到表的后段,后len-m个元素放到表的前段。
注意:交换操作会有多次,每次交换都是在上次交换完成后的顺序表中进行。

Input
第一行输入整数len(1<=len<=1000000),表示顺序表元素的总数;

第二行输入len个整数,作为表里依次存放的数据元素;

第三行输入整数t(1<=t<=30),表示之后要完成t次交换,每次均是在上次交换完成后的顺序表基础上实现新的交换;

之后t行,每行输入一个整数m(1<=m<=len),代表本次交换要以上次交换完成后的顺序表为基础,实现前m个元素与后len-m个元素的交换;

Output
输出一共t行,每行依次输出本次交换完成后顺序表里所有元素。

Sample Input
10
1 2 3 4 5 6 7 8 9 -1
3
2
3
5
Sample Output
3 4 5 6 7 8 9 -1 1 2
6 7 8 9 -1 1 2 3 4 5
1 2 3 4 5 6 7 8 9 -1

//TLE超时代码
#include<bits/stdc++.h>
using namespace std;
int a[1000010];
void creatlist(int n)
{
    for(int i=0; i<=n-1; i++)
    {
        cin>>a[i];
    }
}
void changelist(int n,int m)
{
    int t;
    while(n<m)
    {
        t=a[n];
        a[n]=a[m];
        a[m]=t;
        n++;
        m--;
    }
}
void finally(int m,int len)
{
    changelist(0,len-1);
    changelist(0,len-1-m);
    changelist(len-m,len-1);

}
int main()
{
    int len;
    cin>>len;
    creatlist(len);
    int t;
    cin>>t;
    while(t>0)
    {
        int m;
        cin>>m;
        finally(m,len);
        for(int i=0; i<=len-1; i++)
        {
            if(i==len-1)
            {
                cout<<a[i]<<endl;
            }
            else
            {
                cout<<a[i]<<" ";
            }
        }
        t--;
    }
    return 0;
}

这是我用cout输出cin输入的代码,结果是TLE,而下面的AC代码

//AC代码
#include<bits/stdc++.h>
using namespace std;
int a[1000010];
void creatlist(int n)
{
    for(int i=0; i<=n-1; i++)
    {
        cin>>a[i];
    }
}
void changelist(int n,int m)
{
    int t;
    while(n<m)
    {
        t=a[n];
        a[n]=a[m];
        a[m]=t;
        n++;
        m--;
    }
}
void finally(int m,int len)
{
    changelist(0,len-1);
    changelist(0,len-1-m);
    changelist(len-m,len-1);

}
int main()
{
    ios::sync_with_stdio(false);
    int len;
    cin>>len;
    creatlist(len);
    int t;
    cin>>t;
    while(t>0)
    {
        int m;
        cin>>m;
        finally(m,len);
        for(int i=0; i<=len-1; i++)
        {
            if(i==len-1)
            {
                cout<<a[i]<<endl;
            }
            else
            {
                cout<<a[i]<<" ";
            }
        }
        t--;
    }
    return 0;
}


可以看出这两段代码只有输入输出方式不同,但是提交结果一个AC一个TLE,在竞赛中,遇到大数据时,往往读文件成了程序运行速度的瓶颈,需要更快的读取方式。相信几乎所有的C++学习者都在cin机器缓慢的速度上栽过跟头,于是从此以后发誓不用cin读数据,知道了ios::sync_with_stdio(false);这个可以大幅提高大数据的输入和输出以节省时间

原文地址:https://www.cnblogs.com/youpeng/p/10745542.html