输入输出优化模板

fread

char buf[1<<20],*p1,*p2;
inline char gc()
{
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin))==p1?0:*p1++;
}

输入

template<typename T>
inline void read(T &x)
{
    char tt;
    bool flag=0;
    while(!isdigit(tt=gc())&&tt!='-');
    tt=='-'?(flag=1,x=0):(x=tt-'0');
    while(isdigit(tt=gc())) x=(x<<1)+(x<<3)+(tt^'0');
    if(flag) x=-x;
}

输出

char pbuf[1<<20],*pp=pbuf;
inline void push(const char c)
{
    if(pp-pbuf==(1<<20)) fwrite(pbuf,1,(1<<20),stdout),pp=pbuf;
    *pp++=c;
}

template<typename T>
inline void write(T x)
{
    static int sta[35];
    int top=0;
    if(x<0) push('-'),x=-x;
    do {
        sta[top++]=x%10,x/=10;
    } while(x);
    while(top) push(sta[--top]+'0');
    push('\n');
}

fwrite(pbuf,1,pp-pbuf,stdout);
posted @ 2018-08-22 23:28  Katoumegumi  阅读(241)  评论(0编辑  收藏  举报
返回顶部