版本比较
Fast_IO
正文
增加 69 行删除 38 行
1123456789101112131415161718192021222324252627282930313233343517363738394041424344452346472548492750515230535432333455565758596061373839626364656667686970714546474872737475765077525354555657585960616263646566676878798081828384858687888990919293949596707197989910010175761021031041051061072 行未改动11011111211311411511611711811912012112 行未改动# Fast_IO 2.9# Fast_IO 2.10
2.10 Update:主要修改了说明文档,以及一些小地方。
2.9 Update:压行版压得更死了。
2.8 Update:支持 `cin>> (s+1)`。
## 前言:
快速读入在网上随便便可以搜到,但是大多不够全面(很多只封装了 `int`)/用着不太方便。
因此我写了一个足够足够方便与全面的 Fast_IO,供大家使用。
P.S. 因为封装的原因,常数并未做到巅峰造极,但是在 `1e6` 左右的读入量下,与较快的 Fast_IO 也只有几十毫秒的差距,速度上应该是绰绰有余了。
P.S. 之后准备抛弃 `fread`,基于 `mmap` 再写一个更快的板子,并为 `scanf/printf` 用户实现 `fscanf/fprintf`,敬请期待。
## 简介:
通过 `fread+fwrite` 优化读写常数。
但本 Fast_IO 的核心亮点在于 **全与方便**。
- 提供几乎所有的常见类型的读写(包括但不限于 `__int128` 等非 `std::cin` 的标准类型)。
- 与标准 `std::cin` 读入方式基本一致,方便习惯使用流读写的选手。
- 提供 `a = read<int>()` 或 `read(a)` 的读写方式。
## 功能:
- 使用:
- 输入:`cin>>x` 或 `io>>x`。
- 也可以 `read(x)` 或 `x = read<int>()`。
- 输出:`cin<<x` 或 `io<<x`。
- 也可以 `print(x)`。
- 支持的读入与输出类型:`char, char*, string, bool, double` 和所有的整型变量。
- `double` 输出采用四舍五入制,默认为保留 6 位小数。
- 在第三行可以修改 `precision` 以修改保留位数,或通过函数 `cout/io.SetPrecision(x)` 以修改。
- 请注意,保留位数应在 $[0, 9] $ 位的范围内。
- 支持 `while(cin>> n)`。
- 应部分人需要,支持 `a = read()`,返回值默认为 `long long`。- 注意我 Fast_IO 的启用条件是 `#if defined(ONLINE_JUDGE) or define(FastIO)`。
- 若需修改,请修改 `define putchar(x)` 下的 `inline int64_t read()` 的返回值。 - 默认关闭同步输出。 - 这主要是方便本地调试,以及在 OJ 上提交(提交时会启动)。
- 如果需要强制使用,可以在最前面 `#define FastIO`。
- Fast_IO 开启后,默认关闭同步输出。
- 即答案会在最后一起输出。 - 若想要开启同步输出,请将 `#define putchar(x)` 向上移一行。
- 若想要关闭同步输出,请将 `#define putchar(x)` 这一行删除。
- 若想在不开启 Fast_IO 下仅关闭同步输出,可以把 `#define putchar(x)` 这一行下移一行。
- 更多功能敬请等待,如有建议/需求可以提出。
```cppnamespace fast_IO {#define IOSIZE 100000 static unsigned int precision = 6, POW[10] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000}; // 向下取整到小数点后第 precision 位 static char obuf[IOSIZE], *p3 = obuf;#ifdef ONLINE_JUDGE static char ibuf[IOSIZE], *p1 = ibuf, *p2 = ibuf; unsigned int precision = 6, POW[10] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000}; // 向下取整到小数点后第 precision 位 char obuf[IOSIZE], *p3 = obuf;#if defined(ONLINE_JUDGE) or defined(FastIO) char ibuf[IOSIZE], *p1 = ibuf, *p2 = ibuf;#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))#endif #define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x) inline int64_t read() { int64_t s = 0; int w = 1; char ch; while (ch = getchar(), !(ch>47 and ch<58) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while ((ch>47 and ch<58)) s = s * 10 + ch - 48, ch = getchar(); return s * w; } template<typename T> static inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !(ch>47 and ch<58) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while ((ch>47 and ch<58)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; } template<typename T> static inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); } static inline bool read(char &s) { while (s = getchar(), isspace(s) and s != EOF); return s != EOF; } static inline void print(char x) { putchar(x); } static inline bool read(char *s) { char ch; while (ch = getchar(), (ch<33) && ch != EOF); if (ch == EOF) return false; while (!(ch<33) and (ch != EOF)) *s++ = ch, ch = getchar(); *s = '\0'; return true; } static inline void print(char *x) { while (*x) putchar(*x++); } static inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); } static inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), (ch<33) and ch != EOF); if (ch == EOF) return false; while (!(ch<33)) s += ch, ch = getchar(); return true; } static inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); } static inline bool read(bool &b) { char ch; while(ch=getchar(), (ch<33) and ch != EOF); return ch == EOF ?false :(b=ch^48, true); } static inline void print(bool b) { putchar(b+48); } static inline bool read(double &x) { char ch = getchar(); int f = 1; for(x=0; (ch<48 or 57<ch) and (ch != EOF); ch=getchar()) if(ch == '-') f = -1; if(ch == EOF) return false; for(x=0; 47<ch and ch<58; ch=getchar()) x = x*10 + (ch^48); if(ch != '.') return x *= f, true; double y = 0.1; for(ch=getchar(); 47<ch and ch<58; ch=getchar()) x += y*(ch^48), y /= 10; return x *= f, true; } static inline void print(double x) { if(x < 0) putchar('-'), x = -x; if(!precision) print((unsigned long long)(x-(unsigned long long)(x)>=0.5 ?x+1 :x)); else { unsigned long long xx = x; double y = ((x-xx)*POW[precision]); unsigned long long yy = (y-(unsigned long long)(y)>=0.5 ?y+1 :y); if(yy == POW[precision]) xx++, yy = 0; print(xx), putchar('.'); for(int j=precision-1; ~j; j--) putchar(48+yy/POW[j]%10); } } template<typename T, typename... T1> static inline int read(T& a, T1&... other) { return read(a) + read(other...); } template<typename T, typename... T1> static inline void print(T a, T1... other) { print(a), print(other...); } static struct Fast_IO { #endif template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !(ch>47 and ch<58) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while ((ch>47 and ch<58)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; } template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); } inline bool read(char &s) { while (s = getchar(), isspace(s) and s != EOF); return s != EOF; } inline void print(char x) { putchar(x); } inline bool read(char *s) { char ch; while (ch = getchar(), (ch<33) && ch != EOF); if (ch == EOF) return false; while (!(ch<33) and (ch != EOF)) *s++ = ch, ch = getchar(); *s = '\0'; return true; } inline void print(char *x) { while (*x) putchar(*x++); } inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); } inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), (ch<33) and ch != EOF); if (ch == EOF) return false; while (!(ch<33)) s += ch, ch = getchar(); return true; } inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); } inline bool read(bool &b) { char ch; while(ch=getchar(), (ch<33) and ch != EOF); return ch == EOF ?false :(b=ch^48, true); } inline void print(bool b) { putchar(b+48); } inline bool read(double &x) { char ch = getchar(); int f = 1; for(x=0; (ch<48 or 57<ch) and (ch != EOF); ch=getchar()) if(ch == '-') f = -1; if(ch == EOF) return false; for(x=0; 47<ch and ch<58; ch=getchar()) x = x*10 + (ch^48); if(ch != '.') return x *= f, true; double y = 0.1; for(ch=getchar(); 47<ch and ch<58; ch=getchar()) x += y*(ch^48), y /= 10; return x *= f, true; } inline void print(double x) { if(x < 0) putchar('-'), x = -x; if(!precision) print((unsigned long long)(x-(unsigned long long)(x)>=0.5 ?x+1 :x)); else { unsigned long long xx = x; double y = ((x-xx)*POW[precision]); unsigned long long yy = (y-(unsigned long long)(y)>=0.5 ?y+1 :y); if(yy == POW[precision]) xx++, yy = 0; print(xx), putchar('.'); for(int j=precision-1; ~j; j--) putchar(48+yy/POW[j]%10); } } template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); } template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); } template<typename T> inline T read() { T a; return read(a), a; } struct Fast_IO { bool flag = 1; inline ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } inline void SetPrecision(int x) { if(x > 9) throw runtime_error("Precision too high!"); else if(x < 0) throw runtime_error("Precision too low!"); else precision = x; } // 浮点数精度设为 x,即精确到小数点后 x 位。 inline ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } inline void SetPrecision(int x) { if(x > 9) throw std::runtime_error("Precision too high!"); else if(x < 0) throw std::runtime_error("Precision too low!"); else precision = x; } // 浮点数精度设为 x,即精确到小数点后 x 位。 inline operator bool() { return flag; } } io; Fast_IO& operator >> (Fast_IO &io, char *b) { return io.flag &= read(b), io; } template<typename T> static Fast_IO& operator >> (Fast_IO &io, T &b) { return io.flag &= read(b), io; } template<typename T> static Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; } template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return io.flag &= read(b), io; } template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }#define cout io#define cin io#define endl '\n'} using namespace fast_IO;2 行未改动## 应部分需求,增设压行版:
感谢 [yinianxingkong](https://www.luogu.com.cn/user/748694) 提供压得更死的版本。
- P.S. 是由他人提供的,所以不保证正确性(但应该没有问题)。
- 且版本略旧。
```cppnamespace fast_IO{static unsigned int precision=6,POW[10]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};static char obuf[100000],*p3=obuf;#ifdef ONLINE_JUDGE12 行未改动