设为首页 加入收藏

TOP

输入输出优化(一)
2019-09-14 00:54:13 】 浏览:90
Tags:输入 输出 优化

输入输出优化

  \(C++\) 库里有很多种输入与输出方式,我们最常用的是 \(scanf\)\(printf\)\(cin\)\(cout\) 。除此之外,其它常见的还有: \(getchar\)\(putchar\)\(gets\)\(puts\)\(fgets\)\(fputs\)\(getline\) 等等。当输入或输出的数据量过大时,选择效率更高的输入与输出方式将为我们的程序运行节省不少时间。

基本原理

  一般来说,在输入的效率方面,有 \(cin<<scanf<cin(关闭同步流)<<getchar\) ,具体情况还要看评测环境。不过, \(getchar\) 的效率往往比 \(cin\)\(scanf\) 高得多,因此,基于 \(getchar\)\(putchar\) 的输入输出优化应运而生。

效率分析

  在绝大多数情况下,普通的输入输出优化已足以解决问题。然而,依然有极少数的题目会卡掉普通的输入输出优化。这时,需要使用比 \(getchar\)\(putchar\) 更快的 \(fread\)\(fwrite\)

核心代码

简易版

  整数的输入输出优化。

template<class T>inline void read(T &x)
{
    x=0;register char c=getchar();register bool f=0;
    while(!isdigit(c))f^=c=='-',c=getchar();
    while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
    if(f)x=-x;
}
template<class T>inline void print(T x)
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)print(x/10);
    putchar('0'+x%10);
}
template<class T>inline void print(T x,char c){print(x),putchar(c);}

普通版

  整数、浮点数、字符数组的输入输出优化。

template<class T>inline void read(T &x)
{
    x=0;register char c=getchar();register bool f=0;
    while(!isdigit(c))f^=c=='-',c=getchar();
    while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
    if(f)x=-x;
}
template<class T>inline void readd(T &x)
{
    register ll X=0;register double y=1.0;register char c=getchar();register bool f=0;
    while(!isdigit(c))f^=c=='-',c=getchar();
    while(isdigit(c))X=(X<<3)+(X<<1)+(c^48),c=getchar();
    x=X;
    if(c!='.')return;
    c=getchar();
    while(isdigit(c))x+=(y/=10)*(c^48),c=getchar();
    if(f)x=-x;
}
template<class T>inline void reads(T *x)
{
    register char c=getchar();
    while(c==' '||c=='\n'||c=='\r'||c=='\t')c=getchar();
    while(c!=' '&&c!='\n'&&c!='\r'&&c!='\t'&&c!=EOF)*x++=c,c=getchar();
    *x=0;
}
template<class T>inline void print(T x)
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)print(x/10);
    putchar('0'+x%10);
}
template<class T>inline void printd(T x,ll y)
{

    static ll mul[]={1};
    for(register ll i=1;i<=18;i++)
        mul[i]=(mul[i-1]<<3)+(mul[i-1]<<1);
    if(x<-1e-12)putchar('-'),x=-x;
    x*=mul[y];
    ll x1=(ll)round(x);
    ll x2=x1/mul[y],x3=x1-x2*mul[y];
    print(x2);
    if(y>0)
    {
        putchar('.');
        for(register ll i=1;i<y&&x3*mul[i]<mul[y];putchar('0'),++i);
        print(x3);
    }
}
template<class T>inline void prints(T *x)
{
    while(*x)putchar(*x++);
}
template<class T>inline void print(T x,char c){print(x),putchar(c);}
template<class T>inline void printd(T x,ll y,char c){printd(x,y),putchar(c);}
template<class T>inline void prints(T *x,T c){prints(x),putchar(c);}

加强版

  基于 \(fread\)\(fwrite\) 的输入输出优化。需要注意的是,在使用此优化后,只有读到文件末尾才会停止输入,如果使用键盘输入数据,则需要手动添加 \(EOF\) (在 \(Windows\) 系统下, \(EOF\) 手动输入是按 \(Ctrl+z\) ;在 \(Ubuntu\) 系统下, \(EOF\) 手动输入是按 \(Ctrl+d\) )。

#define getchar gc
#define putchar pc
inline char gc()
{
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
static char buf[100000],*pp=buf;
template<class T>inline void pc(T c)
{
    if(pp-buf==100000)fwrite(buf,1,100000,stdout),pp=buf;
    *pp++=c;
}
inline void fsh(){fwrite(buf,1,pp-buf,stdout);pp=buf;}
template&
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇关于c++模板非类型参数中指针和引.. 下一篇最长上升子序列(LIS: Longest In..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目