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;} //调用示例
本文介绍了两种FastIO的优化方法。一是通过getchar优化输入,它比scanf更快,适用于处理模式串,并能用于__int128的输入输出。二是使用fread函数进行读入优化,给出了具体的操作模板,提升数据读取速度。
1226

被折叠的 条评论
为什么被折叠?



