通用快读快写模板

通用快读快写模板

Use these templates after using namespace std;

Update Info >folded
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Forked from Matrix_mlt[498779]
// + Original (原生功能)
// + Functions
// + read() // 2 Overloaded
// + readchar() // Non-overloaded
// + write() // Non-overloaded
// Pull Request from fengziyi[540226]
// + Submitted (已追加功能)
// + Functions
// + readln() // 2 Overloaded
// + writespace() // 2 Overloaded
// + writeln() // 4 Overloaded
// + New Buffer Reader
// + RePacked
// + Unsubmitted (待完善)
// + Float Number Reading Functions
// + Big Interger Functions
// + ...

Standard Version

>folded
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
namespace IO
{
#define reg register
template<typename _Tp>
inline void read(_Tp& x)
{
x = 0; char c = getchar(); bool f = 0;
while (!std::isdigit(c)) f |= c == 45, c = getchar();
while ( std::isdigit(c)) x = x * 10 + (c ^ 48), c = getchar();
return f ? x = -x : 1, void();
}
template<typename _Tp>
inline void write(_Tp x)
{
static char stk[40]; int top = 0;
if (!x) return putchar(48), void();
if (x < 0) putchar(45), x = -x;
while (x) stk[top++] = x % 10, x /= 10;
while (top) putchar(stk[--top] + 48);
}
// read
template<typename _Tp, typename ...Args>
inline void read(_Tp& x, Args& ...args) { read(x), read(args...); }
}
using namespace IO;

Extend Functions (Must be used with Std Ver)

>folded
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
namespace ExtIO
{
// readchar
inline void readchar(char& x) { for (x = getchar(); !std::isalpha(x); x = getchar()); }

// readln
template<typename _Tp, typename _Tpp>
inline void readln(_Tp a[], _Tpp w) { for (reg _Tpp i = 1; i <= w; ++i) read(a[i]); }
template<typename _Tp, typename _Tpp>
inline void readln(_Tp a[], _Tpp l, _Tpp r) { for (reg _Tpp i = l; i <= r; ++i) read(a[i]); }

// writespace
template<typename _Tp>
inline void writespace(_Tp x) { write(x); putchar(' '); }
template<typename _Tp, typename ...Args>
inline void writespace(_Tp& x, Args& ...args) { writespace(x), writespace(args...); }

// writeln
template<typename _Tp>
inline void writeln(_Tp x) { write(x); putchar('\n'); }
template<typename _Tp, typename ...Args>
inline void writeln(_Tp& x, Args& ...args) { writespace(x), writespace(args...), putchar('\n'); }
template<typename _Tp, typename _Tpp>
inline void writeln(_Tp a[], _Tpp w) { for (reg _Tpp i = 1; i <= w; ++i) writespace(a[i]); putchar('\n'); }
template<typename _Tp, typename _Tpp>
inline void writeln(_Tp a[], _Tpp l, _Tpp r) { for (reg _Tpp i = l; i <= r; ++i) writespace(a[i]); putchar('\n'); }
};
using namespace ExtIO;

Special Buffer-Read Version

(Use flushout() at the end if writeln() is used)

卡常用,高风险

>folded
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <cstring>
namespace bufIO
{
const int _Pu = 1 << 16;
const int _d = 32;
char buf[_Pu], obuf[_Pu];
char *inl = buf + _Pu, *inr = buf + _Pu;
char *outl = obuf, *outr = obuf + _Pu - _d;
inline void flushin()
{
memmove(buf, inl, inr - inl);
int rlen = fread(buf + (inr - inl), 1, inl - buf, stdin);
if (inl - rlen > buf) { buf[inr - inl + rlen] = EOF; }
inl = buf;
}
inline void flushout() { fwrite(obuf, outl - obuf, 1, stdout), outl = obuf; }
template <typename _Tp>
inline void read(_Tp &x)
{
if (inl + _d > inr) { flushin(); }
int isne = 0;
for (; !isdigit(*inl); ++inl) { isne = (*inl == '-'); }
x = (*inl++ - '0');
for (; isdigit(*inl); ++inl) { x = x * 10 + (*inl - '0'); }
if (isne) { x = -x; }
}
template <typename _Tp>
inline void writeln(_Tp x, char end = '\n')
{
if (outl > outr) { flushout(); }
if (x < 0) { *outl++ = '-'; x = -x; }
char sta[20]; char *top = sta;
do { *top++ = (x % 10) + '0'; x /= 10; } while (x);
do { *outl++ = *--top; } while (top != sta);
(*outl++) = end;
}
template<typename _Tp, typename ...Args>
inline void read(_Tp& x, Args& ...args) { read(x), read(args...); }
}
using namespace bufIO;

评论