快读快写

前言

转载自洛谷-黄浩翔大佬的博客

内容

黑科技,稳定、高效率且全面+疯狂常数优化的模板。

DevCpp5.11+Samsung Windows7环境下评测,(编译后运行)读入并输出1802KB字符,本模板耗时1.27ms,getchar和putchar耗时1524.67ms,cincout和scanfprintf直接崩溃。

首先模板献上:
(注意:为避免错误使用,使用前一定要查看后面的使用说明)

#include <bits/stdc++.h>
#define reg register
#define fok (ch!=EOF)
#define sep (ch==' '||ch=='\n'||ch=='\t')
#define dsep !isdigit(ch)
#define neq(a,b) ((a)-(b)>1e-6)
using namespace std;
struct FastIO{
    char rbuf[1000002],wbuf[1000002],b,*p1,*p2;
    int rs,ws,S;
    FastIO(){p1=rbuf,p2=wbuf,S=1000000,rs=1000000,ws=-1,b=1;}
    inline void go(){fwrite(wbuf,1,ws+1,stdout)?ws=-1:0;}
    inline char getch(){
        return rs==S&&
        (p1=rbuf,rs=-1,(S=fread(rbuf,1,S+1,stdin)-1)==-1)?
        (b=0,EOF):(++rs,*p1++);
    }
    inline void putch(int x){
        ws==1000000&&
        (p2=wbuf,ws=-1,fwrite(wbuf,1,1000001,stdout)),++ws,*p2++=x;
    }
    inline void puts(char str[]){
        for(reg int i=0;str[i]!='\0';)putch(str[i]),++i;
    }
    inline void getline(string& s){
        for(reg char ch;(ch=getch())!='\n'&&fok;)s+=ch;
    }
    inline FastIO& operator>>(int& x){
        x=0;reg char f=0,ch=getch();
        while(dsep&&fok)f|=(ch=='-'),ch=getch();
        while(isdigit(ch))
            x=(x<<1)+(x<<3)+(ch^48),ch=getch();
        return x=f?-x:x,*this;
    }
    inline FastIO& operator>>(long long& x){
        x=0;reg char f=0,ch=getch();
        while(dsep&&fok)f|=(ch=='-'),ch=getch();
        while(isdigit(ch))
            x=(x<<1)+(x<<3)+(ch^48),ch=getch();
        return x=f?-x:x,*this;
    }
    inline FastIO& operator>>(char& ch){return ch=getch(),*this;}
    inline FastIO& operator>>(string& s){
        reg char ch=getch();
        while(sep&&fok)ch=getch();
        while(!sep&&fok)s+=ch,ch=getch();
        return *this;
    }
    inline FastIO& operator>>(double& x){
        x=0;reg char f=0,ch=getch();
        double d=0.1;
        while(dsep&&fok)f|=(ch=='-'),ch=getch();
        while(isdigit(ch))x=x*10+(ch^48),ch=getch();
        if(ch=='.')
            while(isdigit(ch=getch()))x+=d*(ch^48),d*=0.1;
        return x=f?-x:x,*this;
    }
    inline FastIO& operator<<(int x){
        reg char ch[10],i=-1;
        !x?(putch('0'),0):0,
        x<0?(putch('-'),x=-x):0;
        while(x)ch[++i]=x%10+48,x/=10;
        while(~i)putch(ch[i]),--i;
        return *this;
    }
    inline FastIO& operator<<(long long x){
        reg char ch[20],i=-1;
        !x?(putch('0'),0):0,
        x<0?(putch('-'),x=-x):0;
        while(x)ch[++i]=x%10+48,x/=10;
        while(~i)putch(ch[i]),--i;
        return *this;
    }
    inline FastIO& operator<<(char ch){return putch(ch),*this;}
    inline FastIO& operator<<(char str[]){return puts(str),*this;}
    inline FastIO& operator<<(string s){
        reg int nn=s.size();
        for(reg int i=0;i<nn;++i)putch(s[i]);
        return *this;
    }
    inline FastIO& operator<<(double x){
        int y=int(x);
        *this<<y;
        x-=y;
        while(neq(x,int(x)))x*=10;
        x?*this<<'.'<<int(x):0;
        return *this;
    }
    inline operator bool(){return b;}
};

原理:每次批量读入1000001字节,存到输入项里,读完了继续尝试读入,存到输入项里。每次批量输出1000001字节,最后用go函数把输出项里剩下的也输出了。如果觉得空间会超限(大约2KB的读写数组,超限的可能还是很小的),请自行调整批量读写的数据大小。

使用说明:

1.本模板的文件流没有兼容用其他的任何输入/输出,包括getchar、putchar、cin、scanf之类。一般情况下该模板足以实现常用的读写功能,如果仍然有特殊需求,请自行添加功能。

2.本模板使用批量输入/输出,输出项中可能留有剩余的不足1000000字节的内容,想要输出这些内容,应调用go函数(无参数)。为了正确且高效率地输出,调用go函数的语句必须加在最后一次输出之后。(一般在main函数return 0前一行)

3.具体用法如下:

· 预处理
注:FastIO io;这句定义最好设置为全局定义!

#include <bits/stdc++.h>//头文件不要乱改,否则会不支持某些函数。
using namespace std;
FastIO io;//全局定义,因为每次定义都会清空文件。故:万万不可反复定义!
int main(){
    freopen(输入文件名,"r",stdin),freopen(输出文件名,"w",stdout);
    //注意在标准输入输出评测时去掉freopen语句
    //调试时一定要用文件读写,否则输入会卡死(评测机上不会)。
    //由于所有评测机本质上皆使用文件式评测,无需担心会在评测机上出错(亲测通过Luogu和OJ评测)。
    return 0;
}

· 输入输出

char ch;int a;long long d;string s1,s2;double aa,bb;
io>>ch>>a>>d>>s1>>aa>>bb,io<<ch<<a<<d<<s1<<aa<<bb;
io.getline(s2),io<<s2,io.puts("123"),io<<"456";
//<<输出操作不能与>>输入操作连在一起!
io.go();//这一句千万别忘了加上!

· 判断EOF(使用重载类型转换运算符实现)

这是本模板的优点,网上的代码均无法正确判断EOF.

while(io>>ch)io<<ch;//判断方法与cin类似。
if(io)...

· 其他

//上面那个struct加在main前面,可以把不要的重载运算符删掉,比如只需要输入输出整数,就把输入输出字符串之类的删掉。
//如果需要别的实现,请以getch和putch代替getchar和putchar,然后自行实现。
//以下几个函数/变量千万不能删掉:
//FastIO,go,getch,putch,rbuf,wbuf,p1,p2,rfile,wfile,rs,ws
//有时编译时可能会有警告,不用担心,可放心使用。
//警告原因:
//1.输出C风格字符串的函数参数不是const类型,输出字符串常量时会警告。
//2.为了常数优化,部分int换成了char,某些编译器会进行警告。

总结

再次鸣谢黄浩翔大佬

猜你喜欢

转载自blog.csdn.net/JH_2002/article/details/81459692
今日推荐