快速 IO

IO 的进化史

cin和cout

刚开始学的时候,老师叫我们用 cin 和 cout
大概是因为这最简单吧
cin>>x;
cout<<x

scanf和printf

学到函数了,老师开始让我们用 scanf 和 printf
也许是这样更方便吧
scanf("%d",&x);
printf("%d",x);

gc和pc

现在,普通的输入不再满足需求
getchar 和 putchar 开始被我们使用

inline int read(){
    int x=0,f=1;char c=getchar();
    for(;c<'0'||c>'9';c=getchar())
        if(c=='-') f=-1;
    for(;c>'/'&&c<':';c=getchar())
        x=x*10+c-'0';
    return x*f;
}
inline void write(int x){
    if(x<0) x=-x,putchar('-');
    if(x>9) write(x/10);
    putchar(x%10+'0');
}
优化

随着我们对位运算的了解,产生了两个优化

inline int read(){
    int x=0,f=1;char c=getchar();
    for(;c<'0'||c>'9';c=getchar())
        if(c=='-') f=-1;
    for(;c>'/'&&c<':';c=getchar())
        x=(x<<1)+(x<<3)+(c^48);
    return x*f;
}

fread和fwrite

听一些大佬说有一题的正解被卡成了 60 分,用了这个才对
于是我自己学习了一下,读入大概就是一次读取一段文件内容到数组中,然后用一个指针获取字符
输出就是把字符存在一个数组中,到了要输出的长度就一次输出

inline char gc(){
    static char buf[100000],*S=buf,*T=buf;
    return S==T&&(T=(S=buf)+fread(buf,1,100000,stdin),S==T)?EOF:*S++;
}
inline int read(){
    static char c=nc();register int f=1,x=0;
    for(;c>'9'||c<'0';c=nc()) c==45?f=-1:1;
    for(;c>'/'&&c<':';c=nc()) x=(x<<3)+(x<<1)+(c^48);
    return x*f;
}
char fwt[100000],*ohed=fwt;
const char *otal=ohed+100000;
inline void pc(char ch){
    if(ohed==otal) fwrite(fwt,1,100000,stdout),ohed=fwt;
    *ohed++=ch;
}
inline void write(int x){
    if(x<0) x=-x,pc('\n');
    if(x>9) write(x/10);
    pc(x%10+'0');
}
fwrite(fwt,1,ohed-fwt,stdout);

注意:这种方法仅用于文件输入输出,测试要添加 freopen



The End

原文地址:https://www.cnblogs.com/KonjakLAF/p/12800363.html