其他分享
首页 > 其他分享> > 快读模板--zhengjun

快读模板--zhengjun

作者:互联网

int,char,double,long long,string,char[] 都可以输入输出
double 的输出方法

double x=123.123;fout<<Precision(5)<<x;

表示保留 5 位小数,默认 6 位。

注意一定要文件读入……

#include<bits/stdc++.h>
using namespace std;typedef long long ll;
struct Precision{int x;Precision(int a){x=a;}};struct SW{int x;SW(int a){x=a;}};struct SF{int x;SF(int a){x=a;}};
#define Set(x,y,z) SW(x)<<SF(y)<<z<<SW(0)
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
#define sid(i,u,a,b) for(int i=a[u],v;v=b[i].to,i;i=b[i].nex)
struct IO{
	#define Tp template<typename T>
	#define _D isdigit(c=gc())
	#define _A f|=eb;if(f)_R
	#define _R return *this
	#define _G p1==p2&&(p2=(p1=B)+fread(B,1,S,stdin),p1==p2)?EOF:*p1
	#define _T(x) while(st[++st[0]]=x%10+'0',x/=10)
	#define _O operator
	#define _P while(st[0])pc(st[st[0]--])
	static const int S=1<<21;char B[S],*p1,*p2,sf,t;int st[105],H,bs,sw;bool eb,f,w;IO(){bs=6;sf=eb=sw=0;}
	IO& _O >> (char&c){_A;while(T(c=gc())){_A;}_R;}char gc(){t=(_G++);t=='\r'&&nex()=='\n'&&(gc());t==EOF&&(eb=1);return t;}
	IO& _O >> (string&s){_A;s="";char c;while(T(c=gc())){_A;}while(s+=c,!T(c=gc()));_R;}void CL(){fwrite(B,1,H,stdout);H=0;}
	IO& _O >> (char*c){_A;while(T(*c=gc())){_A;}while(!T(*++c=gc()));*c=0;_R;}int P(char c){return c=='\n'||c=='\r'||c==EOF;}
	IO& _O >> (double&x){_A;x=0;bool F=0;char c;while(!_D){F^=(c=='-');_A;}while(x=x*10+(c^48),_D&&(P(c),1));if(c^'.')_R;c=gc();
		double k=1;while(x+=(c^48)*(k*=0.1),_D);F&&(x=-x);_R;}int T(char c){return c==' '||P(c);}_O bool()const{return !f;}
	Tp IO& _O >> (T&x){_A;x=0;bool F=0;char c;while(!_D){F^=(c=='-');_A;}while(x=(x<<3)+(x<<1)+(c^48),_D);F&&(x=-x);_R;}
	IO& _O << (const string &s){int l=s.length();pu(sw-l);rep(i,0,l-1)pc(s[i]);_R;}IO& _O << (SF x){sf=x.x;_R;}
	IO& _O << (char*c){int l=strlen(c);pu(sw-l);rep(i,0,l-1)pc(c[i]);_R;}IO& _O << (const char c){pc(c);_R;}~IO(){CL();}
	IO& _O << (const char*c){int l=strlen(c);pu(sw-l);rep(i,0,l-1)pc(c[i]);_R;}void pc(const char c){H==S&&(CL(),0);B[H++]=c;}
	IO& _O << (double x){w=0;x<0&&(w=1,x=-x);double t=0.5;rep(i,1,bs)t*=0.1;x+=t;ll y=x;_T(y);pu(sw-st[0]-w-bool(bs)-bs);
		w&&(pc('-'),0);_P;x-=ll(x);if(bs)pc('.');rep(i,1,bs)pc(int(x*=10)+'0'),x-=int(x);_R;}char nex(){return _G;}
	Tp IO& _O << (T x){x<0&&(pc('-'),x=-x);_T(x);pu(max(sw-st[0],0));_P;_R;}IO& _O << (Precision x){bs=x.x;_R;}
	IO& getline(string&s){_A;s="";char c=gc();if(!P(c))while(s+=c,!P(c=gc()));_R;}IO& _O << (SW x){sw=x.x;_R;}
	IO& getline(char*c){_A;*c=gc();if(!P(*c))while(!P(*++c=gc()));*c=0;_R;}void pu(int x){while(x-->0)pc(sf);}
}fin,fout;
int main(){
	return 0;
}

update2021.11.09:修复了输出小数四舍五入时进位会输出:的问题。

update2021.11.11:增加了while(fin>>x)的功能。

update2021.11.12:增加了输出前导字符的功能:

默认不输出前导,语句:fout<<Set(x,y,z);,表示将 z 补充前导字符 y 使长度大于等于 x。

例子:

fout<<set(10,'0',1.234)<<'\n'<<set(8,'w',"qwert")<<'\n'<<set(2,'*',"!@#$%^&*()");

输出:

001.234000
wwwqwert
!@#$%^&*()

update2021.11.12:令 getline 读取到 \n,\r 为止(如果是空行那就直接退出)。

update:2021.11.12:修复了数据中包含 \r\n 时 getline 的错误。

update:2021.11.16:修改了 #define 中与主程序内容冲突(都加了下划线)。

update:2021.11.16:修改了读入字符串(char[])时,最后一位会成为空格使 strlen 出错的 bug。

update:2021.11.18:修复了 while(fin>>int/double) 时文末为 \n 死循环的 bug。

update:2021.12.23:修复了输出负小数四舍五入的问题。

update:2021.12.23:增加了 repper 的功能。

标签:return,zhengjun,--,char,int,while,gc,快读,define
来源: https://www.cnblogs.com/A-zjzj/p/16366110.html