读入
template <typename _Tp> il void read(_Tp&x) {
char ch;bool flag=0;x=0;
while(ch=getchar(),!isdigit(ch)) if(ch=='-')flag=1;
while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
if(flag) x=-x;
}
下面这个更快,但是要ctrl+z结束多组读入:
inline char nc() {
static char buf[1000000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
template <typename _Tp> inline void read(_Tp&sum) {
char ch=nc();sum=0;
while(!(ch>='0'&&ch<='9')) ch=nc();
while(ch>='0'&&ch<='9') sum=(sum<<3)+(sum<<1)+(ch-48),ch=nc();
}
输出
il void print(int x) {
if(x<0) {x=-x;putchar('-');}
if(x>9) print(x/10);
putchar(x%10+'0');
}
本文介绍了一种在C++中实现快速输入输出的方法,包括使用模板和内联函数优化读取过程,以及通过预读缓冲区提高字符读取效率。此外,还提供了一种输出整数的递归方法。

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



