输入输出优化
更快的读入读出优化
参考代码
namespace IO {
const int MAXSIZE = 1 << 20;
char buf[MAXSIZE], *p1, *p2;
#define gc() \
(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2) \
? EOF \
: *p1++)
inline int rd() {
int x = 0, f = 1;
char c = gc();
while (!isdigit(c)) {
if (c == '-') f = -1;
c = gc();
}
while (isdigit(c)) x = x * 10 + (c ^ 48), c = gc();
return x * f;
}
char pbuf[1 << 20], *pp = pbuf;
inline void push(const char &c) {
if (pp - pbuf == 1 << 20) fwrite(pbuf, 1, 1 << 20, stdout), pp = pbuf;
*pp++ = c;
}
inline void write(int x) {
static int sta[35];
int top = 0;
do {
sta[top++] = x % 10, x /= 10;
} while (x);
while (top) push(sta[--top] + '0');
}
}
使用范例
- 使用
namespace
中的函数,加上using namespace IO
,如果只想单独调用一个函数,使用using IO::rd
类似的rd
处填你想要的函数 - 读入时使用
n = rd()
并且在输入结束后加上EOF
代表文件输入结束。
- 在
dev C++
中,回车后在单行输入Ctrl Z
- 还没有研究出如何在
Clion
中输入EOF