超强快读

· · 个人记录

用于头文件之后 cin->in cout->out

namespace ZDY{}
using namespace ZDY;
using namespace std;

namespace IO{
    const char* ln = "\n";
    const int str = 1 << 20;

    struct IN{
        char buf[str], *s, *t;
        bool _;
        IN(): s(buf), t(buf), _(false){}

        inline char gc(){
            if(s == t){
                t = (s = buf) + fread(buf, 1, str, stdin);
                if(s == t) return EOF;
            }
            return *s++;
        }

        IN& operator>>(char& ch){
            if(_) return *this;
            char c;
            while((c = gc()) != EOF && isspace(c));
            if(c == EOF) _ = true;
            else ch = c;
            return *this;
        }

        IN& operator>>(char* ch){
            memset(ch, 0, sizeof(char) * str);
            if(_) return *this;
            char c;
            while((c = gc()) != EOF && isspace(c));
            if(c == EOF){
                _ = true;
                return *this;
            }
            *ch = c;
            ch++;
            while((c = gc()) != EOF && !isspace(c)){
                *ch = c;
                ch++;
            }
            if(c == EOF) _ = true;
            return *this;
        }

        IN& operator>>(string& ch){
            if(_) return *this;
            char c;
            while((c = gc()) != EOF && isspace(c));
            if(c == EOF){
                _ = true;
                return *this;
            }
            ch += c;
            while((c = gc()) != EOF && !isspace(c)){
                ch += c;
            }
            if(c == EOF) _ = true;
            return *this;
        }

        template<typename T>
        IN& operator>>(T& x){
            if(_) return *this;
            char c = gc();
            bool ff = false;
            while(c != EOF && (c < '0' || c > '9')){
                if(c == '-') ff = !ff;
                c = gc();
            }
            if(c == EOF){
                _ = true;
                return *this;
            }
            x = 0;
            while(c != EOF && '0' <= c && c <= '9'){
                x = (x << 3) + (x << 1) + (c - 48);
                c = gc();
            }
            if(c == EOF) _ = true;
            if(ff) x = -x;
            return *this;
        }
    } in;

    struct OUT{
        char buf[str], *s, *t;
        OUT(): s(buf), t(buf + str){}

        ~OUT(){
            fwrite(buf, 1, s - buf, stdout);
        }

        void pt(char c){
            if(s == t){
                fwrite(s = buf, 1, str, stdout);
                *s++ = c;
            }else{
                *s++ = c;
            }
        }

        OUT& operator<<(const char* s){
            while(*s) pt(*s++);
            return *this;
        }

        OUT& operator<<(char* s){
            while(*s) pt(*s++);
            return *this;
        }

        OUT& operator<<(string s){
            for(int i = 0; s[i]; i++) pt(s[i]);
            return *this;
        }

        template<typename T>
        OUT& operator<<(T x){
            if(!x){
                pt('0');
                return *this;
            }
            if(x < 0){
                pt('-');
                x = -x;
            }
            char a[30];
            int t = 0;
            while(x){
                a[t++] = x % 10;
                x /= 10;
            }
            while(t--) pt(a[t] + '0');
            return *this;
        }
    } out;
}
using namespace IO;