FastIO——读入/输出优化

本文介绍了两种FastIO的优化方法。一是通过getchar优化输入,它比scanf更快,适用于处理模式串,并能用于__int128的输入输出。二是使用fread函数进行读入优化,给出了具体的操作模板,提升数据读取速度。

1.基本读入优化
输入:使用getchar代替scanf对模式串进行处理,调用时为引用
输出:直接调用,输出后没有跟空格或换行,需要手动添加putchar

void read(int &x)
{
    int f=1;x=0;
    char s=getchar();
    while(s<'0'||s>'9')
    {
        if(s=='-')f=-1;           //输入保证是正数时可以省略
        s=getchar();
    }
    while(s>='0'&&s<='9')
    {
        x=x*10+s-'0';
        s=getchar();
    }
    x*=f;
}
void print(int x)
{
    if(x<0)putchar('-'),x=-x;    //输出保证是正数时可以省略
    if(x>9)print(x/10);
    putchar(x%10+'0');
}

除了有快速读入的功能之外,也可用作__int128的输入输出函数使用
2.fread读入优化
直接上模板:

struct ios {
    inline char gc(){
        static const int IN_LEN=1<<18|1;
        static char buf[IN_LEN],*s,*t;
        return (s==t)&&(t=(s=buf)+fread(buf,1,IN_LEN,stdin)),s==t?-1:*s++;
    }

    template <typename _Tp> inline ios & operator >> (_Tp&x){
        static char ch,sgn; ch = gc(), sgn = 0;
        for(;!isdigit(ch);ch=gc()){if(ch==-1)return *this;sgn|=ch=='-';}
        for(x=0;isdigit(ch);ch=gc())x=x*10+(ch^'0');
        sgn&&(x=-x); return *this;
    }
} io;

int main(){io>>a>>b;} //调用示例
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值