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;} //调用示例
版权声明:本文为kaixinqi原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。