• 欢迎光临~

无重载读入输出优化

开发技术 开发技术 2022-11-22 次浏览

仅适用于整形的读入输出,对于浮点数的优化效率不高,对于字符串类型,(fread) 有大概率会出现未知原因错误,不用于读入字符串型。

namespace FIO{
    static char buf[1<<22],*s=buf;
    inline char gc(){
        static char buf[1<<22],*s=buf,*t=buf;
        return s==t&&(t=(s=buf)+fread(buf,1,1<<22,stdin),s==t)?EOF:*s++;
    }
    inline void pc(char c){
        if(s-buf==1<<22)fwrite(buf,1,1<<22,stdout),s=buf;*s++=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){
        if(!x)return pc('0');
        if(x<0)pc('-'),x=-x;
        static int16_t s[44],p=0;
        while(x)s[++p]=x%10,x/=10;
        while(p)pc(s[p--]|48);
    }
    template<class T>inline void put(char c,T x){
        put(x),pc(c);
    }
    template<class T,class...Args>inline void put(char c,T x,Args...args){
        put(c,x),put(c,args...);
    }
    struct OUT{
        inline ~OUT(){
            fwrite(buf,1,s-buf,stdout);
        }
    }out;
}
using FIO::read;
using FIO::put;
程序员灯塔
转载请注明原文链接:无重载读入输出优化
喜欢 (0)
违法和不良信息举报电话:022-22558618 举报邮箱:dljd@tidljd.com