读入输出优化

仅适用于整形的读入输出以及单个字符的输出,对于字符串类型,\(fread\)有大概率会出现未知原因错误,不用于读入字符串型。

namespace FIO{
    constexpr int SZ=1<<22;
    char inbuf[SZ],outbuf[SZ],*s=inbuf,*t=inbuf,*p=outbuf;
    inline char gc(){
        return s==t&&(t=inbuf+fread(s=inbuf,1,SZ,stdin),s==t)?EOF:*s++;
    }
    inline void pc(char c){
        p-outbuf==SZ&&fwrite(p=outbuf,1,SZ,stdout),*p++=c;
    }
    template<class T>inline T read(T&x){
        char c=gc(),f=x=0;
        while(c<'0'||c>'9')f|=c=='-',c=gc();
        while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=gc();
        return x=f?-x:x;
    }
    template<class T,class...Args>inline void read(T&x,Args&...args){
        read(x),read(args...);
    }
    template<class T>inline void put(T x){
        static char s[44],p=0;
        x<0&&(pc('-'),x=-x);
        while(s[++p]=x%10^48,x/=10);
        while(pc(s[p]),--p);
    }
    inline void put(char c){
        pc(c);
    }
    template<class T,class...Args>inline void put(T x,Args...args){
        put(x),put(args...);
    }
    struct OUT{
        inline ~OUT(){
            fwrite(outbuf,1,p-outbuf,stdout);
        }
    }out;
}
posted @ 2022-11-22 06:55  半步蒟蒻  阅读(23)  评论(0)    收藏  举报