1. 基于 getchar/putchar 的快读快写(最常用)
- 整数快读(支持负数)
inline int read(){int x = 0, f = 1;char ch = getchar();while(ch < '0'|| ch > '9'){if(ch == '-') f = -1; ch = getchar();}while(ch >= '0'&& ch <= '9'){ x = x * 10+(ch - '0'); ch = getchar();}return x * f;}
- 无符号整数快读(更快)
inline unsigned int read(){unsigned int x = 0;char ch = getchar();while(ch < '0'|| ch > '9') ch = getchar();while(ch >= '0'&& ch <= '9'){ x = x * 10+(ch - '0'); ch = getchar();}return x;}
- 整数快写(支持负数)
inline void write(int x){if(x < 0){putchar('-'); x = -x;}if(x > 9)write(x / 10);putchar(x % 10+'0');}
2. 基于缓冲区优化的快读(更快)
- 使用静态缓冲区的快读
char buf[1<<21],*p1 = buf,*p2 = buf;
inline char getc(){return p1 == p2 &&(p2 =(p1 = buf)+fread(buf,1,1<<21,stdin), p1 == p2)?EOF:*p1++;}
inline int read(){int x = 0, f = 1;char ch = getc();while(ch < '0'|| ch > '9'){if(ch == '-') f = -1; ch = getc();}while(ch >= '0'&& ch <= '9'){ x = x * 10+(ch - '0'); ch = getc();}return x * f;}
- 使用缓冲区的快写
char pbuf[1<<21],*pp = pbuf;
inline void push(const char& c){if(pp - pbuf ==(1<<21)){fwrite(pbuf,1,(1<<21),stdout); pp = pbuf;}
*pp++= c;}
inline void write(int x){static int sta[35];int top = 0;if(x < 0){push('-'); x = -x;}
do{ sta[top++]= x % 10; x /= 10;}while(x);
while(top)push(sta[--top]+'0');push('\n');// 可以根据需要修改}// 程序结束时调用 flush() 确保所有输出被写入
inline void flush(){fwrite(pbuf,1, pp - pbuf,stdout); pp = pbuf;}
3. 针对不同数据类型的优化实现
- 读取长整型
inline long long readll(){long long x = 0, f = 1;char ch = getchar();while(ch < '0'|| ch > '9'){if(ch == '-') f = -1; ch = getchar();}while(ch >= '0'&& ch <= '9'){ x = x * 10+(ch - '0'); ch = getchar();}return x * f;}
- 读取字符串(一行)
inline void readstr(char* str){char ch = getchar();while(ch == ' '|| ch == '\n') ch = getchar();while(ch != ' '&& ch != '\n'&& ch != EOF){*str++= ch; ch = getchar();}
*str ='\0';}
4. 终极优化版本(竞赛常用)
namespace FastIO {
const int SZ = 1 << 20;
char inbuf[SZ], outbuf[SZ];
int in_left = 0, in_right = 0;
int out_right = 0;
inline void load(){int len = fread(inbuf,1, SZ,stdin); in_left = 0; in_right = len;}
inline char getchar(){if(in_left >= in_right)load();if(in_left >= in_right)return EOF;return inbuf[in_left++];}
inline int read(){int x = 0, f = 1;char ch = getchar();while(ch < '0'|| ch > '9'){if(ch == '-') f = -1; ch = getchar();}while(ch >= '0'&& ch <= '9'){ x = x * 10+(ch - '0'); ch = getchar();}return x * f;}
inline void flush(){fwrite(outbuf,1, out_right,stdout); out_right = 0;}
inline void putchar(char ch){ outbuf[out_right++]= ch;if(out_right == SZ)flush();}
inline void write(int x){if(x < 0){putchar('-'); x = -x;}if(x > 9)write(x / 10);putchar(x % 10+'0');}
struct Flusher{~Flusher(){flush();}} flusher;}
using FastIO::read;
using FastIO::write;
using FastIO::putchar;
- 使用示例
int main(){int n = read;// 读取一个整数
write(n);// 输出一个整数
putchar('\n');// 输出换行// 使用命名空间版本
int m = FastIO::read();
FastIO::write(m);
FastIO::putchar('\n');
return 0;// FastIO::Flusher 会在程序结束时自动调用 flush()}
性能比较
- 基础版 (getchar/putchar):比 scanf/printf 快约 2-3 倍
- 缓冲区版 (fread/fwrite):比基础版快约 2-3 倍
- 终极优化版:比缓冲区版再快约 1.5 倍
在大多数算法竞赛中,基础版或缓冲区版已经足够使用。终极优化版更适合输入数据量特别大的极端情况。
