专栏文章

Fast_IO

科技·工程参与者 10已保存评论 18

文章操作

快速查看文章及其快照的属性,并进行相关操作。

当前评论
18 条
当前快照
2 份
快照标识符
@min4vv6p
此快照首次捕获于
2025/12/01 20:36
3 个月前
此快照最后确认于
2025/12/01 20:36
3 个月前
查看原文

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>>xio>>x
      • 也可以 read(x)x = read<int>()
    • 输出:cin<<xio<<x
      • 也可以 print(x)
  • 支持的读入与输出类型:char, char*, string, bool, double 和所有的整型变量。
    • double 输出采用四舍五入制,默认为保留 6 位小数。
    • 在第三行可以修改 precision 以修改保留位数,或通过函数 cout/io.SetPrecision(x) 以修改。
    • 请注意,保留位数应在 [0,9][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) 这一行下移一行。
  • 更多功能敬请等待,如有建议/需求可以提出。
CPP
namespace fast_IO {
#define IOSIZE 100000
	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++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#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 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> 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;

应部分需求,增设压行版:

感谢 yinianxingkong 提供压得更死的版本。
  • P.S. 是由他人提供的,所以不保证正确性(但应该没有问题)。
  • 且版本略旧。
CPP
namespace 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_JUDGE
static char ibuf[100000],*p1=ibuf,*p2=ibuf;
#define getchar()((p1==p2)&&(p2=(p1=ibuf)+fread(ibuf,1,100000,stdin),p1==p2)?(EOF):(*p1++))
#endif
#define putchar(x)((p3==obuf+100000)&&(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&&ch<58)&&(ch!=EOF))if(ch=='-')w=-1;if(ch==EOF)return 0;while((ch>47&&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&&ch<58)&&(ch!=EOF))if(ch=='-')w=-1;if(ch==EOF)return 0;while((ch>47&&ch<58))s=s*10+ch-48,ch=getchar();return s*=w,1;}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)&&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 0;while(!(ch<33)&&(ch!=EOF))*s++=ch,ch=getchar();*s='\0';return 1;}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)&&ch!=EOF);if(ch==EOF)return 0;while(!(ch<33))s+=ch,ch=getchar();return 1;}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)&&ch!=EOF);return ch==EOF?0:(b=ch^48,1);}static inline void print(bool b){putchar(b+48);}static inline bool read(double&x){char ch=getchar();int f=1;for(;(ch<48||57<ch)&&(ch!=EOF);ch=getchar())if(ch=='-')f=-1;if(ch==EOF)return 0;for(x=0;47<ch&&ch<58;ch=getchar())x=x*10+(ch^48);if(ch!='.')return x*=f,1;double y=0.1;for(ch=getchar();47<ch&&ch<58;ch=getchar())x+=y*(ch^48),y/=10;return x*=f,1;}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{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;}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;}
#define cout io
#define cin io
#define endl '\n'
}using namespace fast_IO;
点个赞支持一下吧!

评论

18 条评论,欢迎与作者交流。

正在加载评论...