class IO {
private:
// 缓冲大小
static const int BUF = 128 * 1024;
// 输入输出流
FILE *in, *out;
char rbuf[BUF], wbuf[BUF], *rs, *rt;
int pos;
// EOF
bool eof;
public:
// fread 和 fwrite
bool load() { return (rt = (rs = rbuf) + fread(rbuf, 1, BUF, in)) == rs;}
void flush() { fwrite(wbuf, 1, pos, out); pos = 0;}
// 构造和析构
IO() { in = stdin, out = stdout, rs = rt = rbuf, pos = 0, eof = 0;}
~IO() { flush();}
// 文件
void open_r(const void* ch) { in = fopen((char*)ch, "r");}
void open_w(const void* ch) { out = fopen((char*)ch, "w");}
void open_r(FILE* p) { in = p;}
void open_w(FILE* p) { out = p;}
IO(const void* ch) { in = fopen((char*)ch, "r"), out = stdout, rs = rt = rbuf, pos = 0, eof = 0;}
IO(FILE* p) { in = p, out = stdout, rs = rt = rbuf, pos = 0, eof = 0;}
bool end() { return eof;}
// 基础操作
inline void pc(const char c) { if(pos >= BUF) flush(); wbuf[pos ++] = c;}
inline char gc() { if(rs == rt) eof = load(); return eof ? EOF : *rs ++;}
// 读入
template<typename T>
inline IO& operator >> (T &x) {
register char c, f = 1;
while(c = gc(), c < '0' || c > '9') if(c == '-') f = ! f;
x = c ^ 48;
while(c = gc(), c >= '0' && c <= '9' && !eof) x = (x << 3) + (x << 1) + (c ^ 48);
return (x = f ? x : -x), *this;
}
inline IO& operator >> (char &c) { return c = gc(), *this;}
inline IO& operator >> (char* c) {
while(*c = gc(), *c == '\n' || *c == '\r' || *c == ' ' || *c == '\t');
while(*++ c = gc(), *c != '\n' && *c != '\r' && *c != ' ' && *c != '\t' && ! eof);
return (*c = '\0'), *this;
}
inline IO& getline(char* c)
{ while(*c = gc(), *c != '\n' && *c != '\r' && ! eof) ++ c; *c = '\0'; return *this;}
inline IO& operator >> (string &s) {
register char c;
while(c = gc(), c == '\n' || c == '\r' || c == ' ' || c == '\t');
s = c;
while(c = gc(), c != '\n' && c != '\r' && c != ' ' && c != '\t') s += c;
return *this;
}
inline IO& operator >> (long double &x) {
register char c, op = 1; long double e = 1;
while(c = gc(), ! isdigit(c)) if(c == '-') op = ! op;
x = c ^ 48;
while(c = gc(), isdigit(c)) x = (x * 10) + (c ^ 48);
if(c == '.') while(e /= 10, c = gc(), isdigit(c)) x += (c ^ 48) * e;
return (x = op ? x : -x), *this;
}
inline IO& operator >> (double &x) {
long double y; *this >> y; return x = y, *this;
}
// 输出
template<typename T>
inline IO& operator << (T x) {
if(x < 0) pc('-'), x = -x;
register T y = 10;
while(y < x) y = (y << 3) + (y << 1);
while(y /= 10) pc((x / y) ^ 48), x %= y;
return *this;
}
inline IO& operator << (const char c) { return pc(c), *this;}
inline IO& operator << (const char* c) { while(*c) pc(*c ++); return *this;}
//inline IO& operator << (char c) { return pc(c), *this;}
inline IO& operator << (char* c) { while(*c) pc(*c ++); return *this;}
inline IO& putline(char* c) { while(*c) pc(*c ++); return pc('\n'), *this;}
inline IO& operator << (string s) { return *this << s.c_str();}
template<typename T>
inline void print(T x, short y) {
T t; *this << (int)(t = floor(x));
if(! y) return;
pc('.');
while(y) { x -= t, x *= 10; *this << (int)floor(x); y --;}
}
};
转载于:https://www.cnblogs.com/akakw1/p/10255357.html