版本比较
Fast_IO
正文
增加 15 行删除 15 行
31 行未改动3233343536363738383940404142434445464647484849505051525253545556565758585960616262636464656666676869703 行未改动747576777878798081828 行未改动919293949595969797989910010132 行未改动31 行未改动
- 使用:
- 输入:`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)`。
- 注意我 Fast_IO 的启用条件是 `#if defined(ONLINE_JUDGE) or define(FastIO)`。
- 这主要是方便本地调试,以及在 OJ 上提交(提交时会启动)。
- 如果需要强制使用,可以在最前面 `#define FastIO`。
- Fast_IO 开启后,默认关闭同步输出。
- 即答案会在最后一起输出。
- 若想要关闭同步输出,请将 `#define putchar(x)` 这一行删除。
- 若想在不开启 Fast_IO 下仅关闭同步输出,可以把 `#define putchar(x)` 这一行下移一行。
- 更多功能敬请等待,如有建议/需求可以提出。
```cppnamespace fast_IO {3 行未改动#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++))#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)#endif #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); }8 行未改动 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 { struct Fast_IO { bool flag = 1; inline ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } 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; }32 行未改动