【模板】快读快输

updated on 2023.9.13

namespace RobinChen{
    char buf[1<<20],*p1=buf,*p2=buf;
    int gc(){
        if(p1==p2) p2=(p1=buf)+fread(buf,1,1<<20,stdin);
        return p1==p2?EOF:*p1++;
    }
    template<class T> T read(){
        T x=0; char ch=gc(); int flg=1;
        for(;ch<'0'||'9'<ch;ch=gc()) if(ch=='-') flg=-1;
        for(;'0'<=ch&&ch<='9';ch=gc()) x=x*10+ch-'0';
        return x*flg;
    }
};
using RobinChen::read;
namespace Crxis{
    char buf[1<<20],*p1=buf,*p2=buf+(1<<20);
    void flush(){fwrite(buf,1,p1-buf,stdout),p1=buf;}
    void pc(char ch){
        if(p1==p2) flush();
        *p1++=ch;
    }
    void write(int x){
        if(x==0) return pc('0'),void();
        if(x<0) pc('-'),x=-x;
        static int stk[40];
        int top=0;
        while(x) stk[++top]=x%10,x/=10;
        for(int i=top;i>=1;i--) pc(stk[i]+'0');
    }
};
using Crxis::pc;
using Crxis::write;
using Crxis::flush;

posted on 2021-02-16 22:40:01 | under 学术 | source

警示后人:不要试图去写过于复杂的快读快输,卡常/CE 会让你崩溃

一行式:

const char endl='\n';namespace IO{const int n=1<<20;char i[n],o[n],*ip=i,*iq=i,*op=o,*oq=o+n;char G(){return ip==iq&&(iq=(ip=i)+std::fread(i,1,n,stdin)),ip==iq?EOF:*ip++;}void P(char c){op==oq&&std::fwrite(op=o,1,n,stdout),*op++=c;}bool D(char c){return '0'<=c&&c<='9';}struct F{~F(){std::fwrite(o,1,op-o,stdout);}}f;struct R{template<class T>R operator>>(T&x){x=0;int f=0,c=G();for(;!D(c);c=G())f|=c=='-';for(;D(c);c=G())x=x*10-'0'+c;if(f)x=-x;return*this;}}cin;struct W{template<class T>W operator<<(T x){if(x<0)x=-x,P('-');if(x>9)*this<<x/10;P(x%10+'0');return*this;}}cout;template<>W W::operator<<(char c){P(c);return*this;}};using IO::cin;using IO::cout;
//

重载运算符式:https://www.luogu.com.cn/paste/7clcx6a0

#include <cstdio>
//#define DEBUG
#define NO_IOSTREAM
namespace FAST_IO{
    static const int 
        _MAX_IO_SIZE=1<<15,
        _MAX_NUMBER_LENGTH=20;
    class IO_chars{
        private: char _I_buf[_MAX_IO_SIZE],*_I_p1,*_I_p2;
        private: char _O_buf[_MAX_IO_SIZE],*_O_p1,*_O_p2;
#ifndef DEBUG
        public: inline IO_chars():_I_p1(_I_buf),_I_p2(_I_buf),_O_p1(_O_buf),_O_p2(_O_buf+_MAX_IO_SIZE){}
        public: inline ~IO_chars(){std::fwrite(_O_buf,1,_O_p1-_O_buf,stdout);}
        public: inline char getchar(){
            if(_I_p1==_I_p2){
                _I_p1=_I_buf;
                _I_p2=_I_p1+std::fread(_I_buf,1,_MAX_IO_SIZE,stdin);
            }
            return _I_p1==_I_p2?EOF:*_I_p1++;
        }
        public:inline void putchar(const char &_O_ch){
            if(_O_p1==_O_p2){
                std::fwrite(_O_buf,1,_MAX_IO_SIZE,stdout);
                _O_p1=_O_buf;
            }
            *_O_p1++=_O_ch;
        }
#else
        public: inline char getchar(){return std::getchar();}
        public:inline void putchar(const char &_O_ch){std::putchar(_O_ch);}
#endif
    } ioch;
    class IO{
        public: inline bool isdigit(const char &ch){return '0'<=ch&&ch<='9';}
        public: inline bool isblank(const char &ch){return ch<=32||ch==127;}
#define def(type) \
        public: inline IO operator>>(type &x){\
            bool f;char ch=ioch.getchar();\
            for(f=0;!isdigit(ch);f|=(ch=='-'),ch=ioch.getchar())\
                if(ch==EOF) throw "Runtime error: No any numbers to read.";\
            for(x=0; isdigit(ch);ch=ioch.getchar())\
                x=(x<<3)+(x<<1)+(ch^'0');\
            return (f)&&(x=~x+1),*this;\
        }
        def(short) def(int) def(unsigned int)
        def(long long) def(unsigned long long)
#undef def
#define def(type) \
        public: inline IO operator<<(type x){\
            short stk[_MAX_NUMBER_LENGTH],cnt=0;\
            if(x<0) x=~x+1,ioch.putchar('-');\
            do stk[cnt++]=x%10; while(x/=10);\
            do ioch.putchar(stk[--cnt]+'0'); while(cnt);\
            return *this;\
        }
        def(short) def(int) def(unsigned int)
        def(long long) def(unsigned long long)
#undef def
        public: inline IO operator>>(char &a){
            char ch=ioch.getchar();
            for(; isblank(ch);ch=ioch.getchar())
                if(ch==EOF) return a=ch,*this;
            return a=ch,*this;
        }
        public: inline IO operator<<(const char &a){
            ioch.putchar(a);
            return *this;
        }
        public: inline IO operator>>(char *a){
            char ch=ioch.getchar(); 
            for(; isblank(ch);ch=ioch.getchar())
                if(ch==EOF) throw "Runtime error: No any chars to read.";
            for(;!isblank(ch);ch=ioch.getchar())
                *a++=ch;
            return *a='\0',*this;
        }
        public: inline IO operator<<(const char *a){
            for(;*a;a++)
                ioch.putchar(*a);
            return *this;
        }
    } io;
#ifdef DEBUG
#undef DEBUG
#endif
#ifdef NO_IOSTREAM
#define cin io
#define cout io
#define endl '\n'
#endif
}
using namespace FAST_IO;
int main(){
    int a,b;
    cin>>a>>b;
    cout<<a+b;
    return 0;
}

函数式:

#include <cstdio>
namespace FAST_IO{
    //#define DEBUG
    class IO_chars{
        private:
            static const int _MAX_IO_SIZE=1<<20;
            char _I_buf[_MAX_IO_SIZE],*_I_p1,*_I_p2;
            char _O_buf[_MAX_IO_SIZE],*_O_p1,*_O_p2;
        public:
            #ifndef DEBUG
            IO_chars():_I_p1(_I_buf),_I_p2(_I_buf),_O_p1(_O_buf),_O_p2(_O_buf+_MAX_IO_SIZE){}
            ~IO_chars(){std::fwrite(_O_buf,1,_O_p1-_O_buf,stdout);}
            #endif
            char getchar(){
                #ifdef DEBUG
                return std::getchar();
                #endif
                if(_I_p1==_I_p2){
                    _I_p1=_I_buf;
                    _I_p2=_I_p1+std::fread(_I_buf,1,_MAX_IO_SIZE,stdin);
                }
                return _I_p1==_I_p2?EOF:*_I_p1++;
            }
            void putchar(const char &_O_ch){
                #ifdef DEBUG
                std::putchar(_O_ch);
                return ;
                #endif
                if(_O_p1==_O_p2){
                    std::fwrite(_O_buf,1,_MAX_IO_SIZE,stdout);
                    _O_p1=_O_buf;
                }
                *_O_p1++=_O_ch;
            }
    } IOER;
    class IO{
        private:
            static const int _MAX_LEN=20,_ZERO=(int)'0';
            int stack[_MAX_LEN];
        public:
            bool isdigit(const char &ch){return '0'<=ch&&ch<='9';}
            bool isblank(const char &ch){return ch==' '||ch==EOF||ch=='\n'||ch=='\r'||ch=='\t';}
            template<typename T>
            inline void read(T &x){
                #ifdef DEBUG
                std::scanf("%d",&x);
                return ;
                #endif
                x=0;int f=0;char ch=IOER.getchar();
                for(;!isdigit(ch);f|=(ch=='-'),ch=IOER.getchar())
                    if(ch==EOF) return ;
                for(; isdigit(ch);ch=IOER.getchar())
                    x=(x<<3)+(x<<1)+(ch^_ZERO);
                if(f) x=(~x+1);
            }
            template<typename T>
            inline void write(const T &x){
                #ifdef DEBUG
                std::printf("%d",x);
                return ;
                #endif
                int num=x,top=0;
                if(num<0) num=(~num+1),IOER.putchar('-');
                do stack[top++]=num%10,num/=10; while(num);
                while(top) IOER.putchar(stack[--top]+_ZERO);
            }
            inline void readstr(char *a){
                #ifdef DEBUG
                std::scanf("%s",a);
                return ;
                #endif
                int i=0;char ch=IOER.getchar();
                for(; isblank(ch);ch=IOER.getchar())
                    if(ch==EOF) return ;
                for(;!isblank(ch);ch=IOER.getchar())
                    a[i++]=ch;
                a[i]='\0';
            }
            inline void gets(char *a){
                #ifdef DEBUG
                std::scanf("%[^\n\r]",a);
                //不知道这种方式读入的字符串有没有\0,没有那就换成std::gets吧
                return ;
                #endif
                int i=0;char ch=IOER.getchar();
                for(; (ch==EOF||ch=='\n'||ch=='\r');ch=IOER.getchar())
                    if(ch==EOF) return ;
                for(;!(ch==EOF||ch=='\n'||ch=='\r');ch=IOER.getchar())
                    a[i++]=ch;
                a[i]='\0';
            }
            inline void puts(const char *a){
                #ifdef DEBUG
                std::puts(a);
                return ;
                #endif
                for(int i=0;a[i]!='\0';i++)
                    IOER.putchar(a[i]);
                IOER.putchar('\n');
            }
    } io;
    //#undef DEBUG
}
using FAST_IO::io;
int x;
char a[10010];
int main(){
    io.read(x);
    io.write(x);
    io.puts("");
    io.gets(a);
    io.puts(a);
    return 0;
}
posted @ 2023-09-13 14:44  caijianhong  阅读(49)  评论(0编辑  收藏  举报