fastIO

a___

2020-10-06 16:29:10

Personal

整形快读(不定参,c++11)

#include<cstdio>
char getch()
{
    static const int N=1e6;
    static char buf[N+10],*p1=NULL,*p2=NULL;
    return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,N,stdin),p1==p2)?EOF:*p1++;
}
void putch(char c)
{
    #define flush() fwrite(buf,1,len,stdout),len=0
    static const int N=1e6;
    static char buf[N+10];
    static int len=0;
    if(c==EOF)flush();
    else buf[len++]=c;
    if(len==N)flush();
    #undef flush
}
void readint(){}
template<typename T,typename ...Tr>
void readint(T &a,Tr&... rest)
{
    a=0;bool f=false;char ch;
    while(ch=getch(),ch<'0'||ch>'9')f=ch=='-';
    do a=a*10+ch-'0',ch=getch(); while(ch>='0'&&ch<='9');
    if(f)a=-a;
    readint(rest...);
}
template<typename T>
void writeaninteger(T a)
{
    static char buf[20],len=0;
    if(a<0)putch('-'),a=-a;
    do buf[len++]=a%10+'0',a/=10; while(a);
    while(len)putch(buf[--len]);
}
template<const char end='\n',const char split=' ',typename T>
void writeint(T a)
{
    writeaninteger(a);
    if(end)putch(end);
}
template<const char end='\n',const char split=' ',typename T,typename ...Tr>
void writeint(T a,Tr... rest)
{
    writeaninteger(a);
    if(split)putch(split);
    writeint<end,split>(rest...);
}
int main()
{
    int a;long long b;
    readint(a,b);
    writeint<' ','\r'>(a,b,999);
    putch(EOF);
    return 0;
}

重载快读(附带几个常用类型)

#include<cstdio>
namespace qio
{
    class istream
    {
        private:
            FILE *fin;
            static const int N=1e6;
            char buf[N+10],*p1=NULL,*p2=NULL;
        protected:
            inline char getchar(void){return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,N,fin),p1==p2)?EOF:*p1++;}
            template<typename T>
            void readint(T &a)
            {
                a=0;bool f=false;char ch;
                while(ch=getchar(),ch<'0'||ch>'9')f=ch=='-';
                do a=a*10+ch-'0',ch=getchar(); while(ch>='0'&&ch<='9');
                if(f)a=-a;
            }
            inline void readstr(char *p)
            {
                do *p=getchar(); while(*p<=32);
                do *++p=getchar(); while(*p>32);
                *p='\0';
            }
        public:
            istream():fin(NULL){}
            istream(FILE *f):fin(f){}
            istream(const char *name):fin(fopen(name,"r")){}
            template<typename T>
            istream& operator>> (T &a){readint(a);return *this;}
            istream& operator>> (char &c){c=getchar();return *this;}
            istream& operator>> (char *s){readstr(s);return *this;}
    };
    class ostream
    {
        private:
            FILE *fout;
            static const int N=1e6;
            char buf[N+10];int len;
        protected:
            inline void flush(){fwrite(buf,1,len,fout),len=0;}
            inline void putchar(char c){buf[len++]=c;if(len==N)flush();}
            template<typename T>
            void writeint(T a)
            {
                char buf[20],len=0;
                if(a<0)putchar('-'),a=-a;
                do buf[len++]=a%10+'0',a/=10; while(a);
                while(len)putchar(buf[--len]);
            }
            inline void writestr(const char *s){for(int i=0;s[i];++i)putchar(s[i]);}
        public:
            ostream():fout(NULL){}
            ostream(FILE *f):fout(f){}
            ostream(const char *name):fout(fopen(name,"w")){}
            ~ostream(){flush();}
            template<typename T>
            ostream& operator<< (T a){writeint(a);return *this;}
            ostream& operator<< (const char c){putchar(c);return *this;}
            ostream& operator<< (char *s){writestr(s);return *this;}
            ostream& operator<< (const char *s){writestr(s);return *this;}
    };
    istream cin(stdin);
    ostream cout(stdout);
    const char endl='\n',sep=' ';
}
int main()
{
    char s[10];
    qio::istream input("1.txt");
    qio::ostream output("2.txt");
    input>>s;
    output<<1234<<qio::endl;
    return 0;
}

update:C版不定参快读

#include<bits/stdc++.h>
namespace fastIO
{
    char getch()
    {
        static const int N=1e6;
        static char buf[N+10],*p1=NULL,*p2=NULL;
        return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,N,stdin),p1==p2)?EOF:*p1++;
    }
    void putch(char c)
    {
        #define flush() fwrite(buf,1,len,stdout),len=0
        static const int N=1e6;
        static char buf[N+10];
        static int len=0;
        if(c==EOF)flush();
        else buf[len++]=c;
        if(len==N)flush();
        #undef flush
    }
    inline long long getint()
    {
        long long a=0;bool f=false;char ch;
        while(ch=getch(),ch<'0'||ch>'9')f=ch=='-';
        do a=a*10+ch-'0',ch=getch(); while(ch>='0'&&ch<='9');
        return f?-a:a;
    }
    void qscanf(char *fmt, ...)
    {
        va_list ap;
        va_start(ap,fmt);
        for(char *p=fmt;*p;++p)
        if(*p=='%')
        {
            switch(*++p)
            {
                case 'd':(*(va_arg(ap,int*)))=getint();break;
                case 'l':(*(va_arg(ap,long long*)))=getint();break;
                case 'c':(*(va_arg(ap,char*)))=getch();break;
                default:break;
            }
        }
        va_end(ap);
    }
    inline void putint(long long a)
    {
        static char buf[20],len=0;
        if(a<0)putch('-'),a=-a;
        do buf[len++]=a%10+'0',a/=10; while(a);
        while(len)putch(buf[--len]);
    }
    void qprintf(char *fmt, ...)
    {
        va_list ap;
        char *p;
        va_start(ap, fmt);
        for(char *p=fmt;*p;++p)
        if(*p=='%')
        {
            switch(*++p)
            {
                case 'd':putint(va_arg(ap,int));break;
                case 'l':putint(va_arg(ap,long long));break;
                case 'c':putch(va_arg(ap,int));break;
                default:putch(*p);break;
            }
        }
        else putch(*p);
        va_end(ap);
    }
    void qflush(){putch(EOF);}
}
using fastIO::qscanf;
using fastIO::qprintf;
using fastIO::qflush;
int main()
{
    int a;long long b;
    qscanf("%d%l",&a,&b);
    qprintf("%d %l\n",a,b);
    qflush();
    return 0;
}

几种IO速度比较:
数据量:3e7;数据类型:整形;编译环境:不开O2.
input:

scanf 18.632s
cin开流同步 21.921s
cin关流同步 6.702s
c++11qread 1.256s
qcin 1.450s
普通fread 1.107s

output:

printf 62.680s
cout开流同步 4.911s
cout关流同步 5.956s
c++11qput 1.980s
qcout 2.235s
普通fwrite 1.946s