轻量化快读
namespace fast_read{
char buf[1000005], *s = buf, *t = buf, ch, f;
#define gc() s == t && (t = (s = buf) + fread(buf, 1, 1000000, stdin), s == t) ? EOF : *s ++
template <typename T>
inline void read(T &x)
{
x = f = 0, ch = gc();
while(ch < '0' || ch > '9')
{
if(ch == '-') f = 1;
ch = gc();
}
while('0' <= ch && ch <= '9') x = x * 10 + ch - 48, ch = gc();
f && (x = -x);
}
};
using fast_read::read;
封装快读快写
更接近于 cin
和 cout
的使用。
代码长度缩短了。
几个调整输入方式的成员函数:
- char
chnormal
(默认):输入一个可见字符。chall
:输入任意一个字符。chdigit
:输入一个数字字符。chletter
:输入一个字母字符,包括大小写。
- char*/string
strnormal
(默认):输入一个字符串,直到不可见字符和空格结束。strline
:输入一个字母串,包含空格。
- int
intnormal
(默认):输入有符号整数。intunsigned
:输入无符号整数。
- long long
llnormal
(默认):输入有符号长整数。llunsigned
:输入无符号长整数。
同样可以通过在本定宏定义 LOCAL
进行调试。
调用示例:
int main()
{
char x, y[3];
int a;
unsigned b;
IO.strline();
IO >> x >> y >> a >> b;
IO << x << IO.endl << y << IO.endl << a << IO.endl << b << IO.endl;
return 0;
}
警告:可能出 bug,最好不要滥用。
目前浮点数的读入可能出一些精度问题。
代码
struct fast_IO{
/***read***/
char buf[1000000], *s = buf, *t = buf;
inline char gc()
{
#ifdef LOCAL
return getchar();
#endif
return s == t && (t = (s = buf) + fread(buf, 1, 1000000, stdin), s == t) ? EOF : *s ++ ;
}
// read a character
int chtype = 0;
inline void chnormal() {chtype = 0;}
inline void chall() {chtype = 1;}
inline void chdigit() {chtype = 2;}
inline void chletter() {chtype = 3;}
inline char chread()
{
char ch = gc();
while(ch != EOF && ch <= 32) ch = gc();
return ch;
}
inline char digitread()
{
char ch = gc();
while(ch != EOF && (ch < '0' || ch > '9')) ch = gc();
return ch;
}
inline char letterread()
{
char ch = gc();
while(ch != EOF && !('A' <= ch && ch <= 'Z' || 'a' <= ch && ch <= 'z')) ch = gc();
return ch;
}
// read a string
int strtype = 0;
inline void strnormal() {strtype = 0;}
inline void strline() {strtype = 1;}
inline void strread(char *s)
{
char ch = gc();
while(ch <= 32) ch = gc();
while(ch > 32) *s ++ = ch, ch = gc();
*s = 0;
}
inline void lineread(char *s)
{
char ch = gc();
while(ch < 32) ch = gc();
while(ch >= 32) *s ++ = ch, ch = gc();
*s = 0;
}
// read an integer
int inttype = 0;
inline void intnormal() {inttype = 0;}
inline void intunsigned() {inttype = 1;}
int lltype = 0;
inline void llnormal() {lltype = 0;}
inline void llunsigned() {lltype = 1;}
template <typename T>
inline void uread(T &x)
{
char ch = gc();
x = 0;
while(ch < '0' || ch > '9') ch = gc();
while('0' <= ch && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc();
}
template <typename T>
inline void read(T &x)
{
char ch = gc();
x = 0;
bool f = 0;
while(ch < '0' || ch > '9')
{
if(ch == '-') f = 1;
ch = gc();
}
if(f) while('0' <= ch && ch <= '9') x = x * 10 - (ch ^ 48), ch = gc();
else while('0' <= ch && ch <= '9') x = x * 10 + (ch ^ 48), ch = gc();
}
// read a real number
template <typename T>
inline void dread(T &x)
{
char ch = gc();
x = 0;
int f = 1;
while(ch < '0' || ch > '9')
{
if(ch == '-') f = -1;
ch = gc();
}
while('0' <= ch && ch <= '9') x = x * 10 + (ch ^ 48), ch = gc();
if(ch == '.')
{
ch = gc();
double p = 0.1;
while('0' <= ch && ch <= '9') x += p * (ch ^ 48), ch = gc(), p *= 0.1;
}
x *= f;
}
inline fast_IO & operator >> (char &x)
{
switch(chtype)
{
case 0 : x = chread(); break ;
case 1 : x = gc(); break ;
case 2 : x = digitread(); break ;
case 4 : x = letterread(); break ;
default : break ;
}
return *this;
}
inline fast_IO & operator >> (char *x)
{
strtype ? lineread(x) : strread(x);
return *this;
}
inline fast_IO & operator >> (string &x)
{
static char buf[1000005];
strtype ? lineread(buf) : strread(buf);
return x = buf, *this;
}
inline fast_IO & operator >> (int &x)
{
inttype ? uread(x) : read(x);
return *this;
}
inline fast_IO & operator >> (unsigned &x)
{
return uread(x), *this;
}
inline fast_IO & operator >> (long long &x)
{
lltype ? uread(x) : read(x);
return *this;
}
inline fast_IO & operator >> (unsigned long long &x)
{
return read(x), *this;
}
inline fast_IO & operator >> (__int128 &x)
{
lltype ? read(x) : read(x);
return *this;
}
inline fast_IO & operator >> (unsigned __int128 &x)
{
return read(x), *this;
}
inline fast_IO & operator >> (float &x)
{
return dread(x), *this;
}
inline fast_IO & operator >> (double &x)
{
return dread(x), *this;
}
inline fast_IO & operator >> (long double &x)
{
return dread(x), *this;
}
inline fast_IO & operator >> (__float128 &x)
{
return dread(x), *this;
}
/***write***/
char obuf[1000000], *p = obuf;
const char endl = '\n';
inline void pc(char x)
{
p - obuf < 1000000 ? (*p ++ = x) : (fwrite(obuf, p - obuf, 1, stdout), p = obuf, *p ++ = x);
}
inline ~fast_IO() {fwrite(obuf, p - obuf, 1, stdout);}
template <typename T>
inline void write(T x)
{
if(!x) return pc(48);
int len = 0;
static char c[40];
if(x < 0) pc('-'), x = -x;
while(x) c[ ++ len] = (x % 10) ^ 48, x /= 10;
while(len) pc(c[len -- ]);
}
inline fast_IO & operator << (int x)
{
return write(x), *this;
}
inline fast_IO & operator << (unsigned x)
{
return write(x), *this;
}
inline fast_IO & operator << (long long x)
{
return write(x), *this;
}
inline fast_IO & operator << (unsigned long long x)
{
return write(x), *this;
}
inline fast_IO & operator << (__int128 x)
{
return write(x), *this;
}
inline fast_IO & operator << (unsigned __int128 x)
{
return write(x), *this;
}
inline fast_IO & operator << (char x)
{
return pc(x), *this;
}
inline fast_IO & operator << (char *x)
{
while(*x) pc(*x ++ );
return *this;
}
inline fast_IO & operator << (string x)
{
for(char i : x) pc(i);
return *this;
}
}IO;