封装普通快读

你还在因背不下来快读而烦恼吗?
你还在因突然发现代码需要快读,然而改起来很麻烦而烦恼吗?
快使用我们的封装快读,近乎完美兼容 cin
目前支持:整形全家桶(intshortlonglong long__int128),double(如要 long double 在代码里加入 #define double long double),stringchar

食用方法:

  1. 首先保证你代码输入用的 cin
  2. 如果代码出现形如 while(cin>>x) 的代码,复制代码 22,否则复制代码 11(有于代码 22 里含有大量 if,会导致效率降低,但只有这样才可以支持 while(cin>>x) 所以立了两个版本)。
  3. 粘贴代码。

然后你就会神奇的发现,你的 cin 变成快读啦 (●'◡'●)。

代码1:

#define cin fastCin
struct fastCin_v1_0_1{
#define gc() getchar()
inline fastCin_v1_0_1&operator>>(char&x){x=gc();return*this;}
inline fastCin_v1_0_1&operator>>(char*x){char ch=gc();while(ch==' '||ch=='\n'||ch=='\t') ch=gc();while(ch!=' '&&ch!='\n'&&ch!='\t')*x=ch,x++,ch=gc();*x='\0';return*this;}
inline fastCin_v1_0_1&operator>>(string&x){char ch=gc();while(ch==' '||ch=='\n'||ch=='\t')ch=gc();while(ch!=' '&&ch!='\n'&&ch!='\t')x+=ch,ch=gc();return*this;}
inline fastCin_v1_0_1&operator>>(double&x){x=0;int sign=1;char ch=gc();while(ch<'0'||ch>'9'){if(ch=='-')sign=-1;ch=gc();}while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=gc();if (ch =='.'){ch=gc();double cnt=0.1;while(ch>='0'&&ch<='9')x=x+(ch-'0')*cnt,cnt/=10,ch=gc();}x=x*sign;return*this;}
template<typename _Tp>
inline fastCin_v1_0_1&operator>>(_Tp&x){x=0;int sign=1;char ch=gc();while(ch<'0'||ch>'9'){if(ch=='-')sign=-1;ch = gc();}while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=gc();x=x*sign;return*this;}
}fastCin;

代码2:

#define cin fastCin
struct fastCin_v1_0_1{
bool boo=1;
inline char gc(){char ch=getchar();if(ch == EOF)boo=0;return ch;}
operator bool(){return boo;}
inline fastCin_v1_0_1&operator>>(char&x){x=gc();return*this;}
inline fastCin_v1_0_1&operator>>(char*x){char ch=gc();if(ch==EOF)return*this;while(ch==' '||ch=='\n'||ch=='\t'){ch=gc();if(ch==EOF)return*this;}while(ch!=' '&&ch!='\n'&&ch!='\t')*x=ch,x++,ch=gc();*x='\0';return*this;}
inline fastCin_v1_0_1&operator>>(string & x){char ch=gc();if(ch==EOF)return*this;while(ch==' '||ch=='\n'||ch=='\t') ch=gc();while(ch!=' '&&ch!='\n'&&ch!='\t'){x+=ch,ch=gc();if(ch==EOF)return*this;}return*this;}
inline fastCin_v1_0_1 & operator>>(double&x){x=0;int sign=1;char ch=gc();if(ch==EOF)return*this;while(ch<'0'||ch>'9'){if(ch=='-')sign=-1;ch=gc();if(ch==EOF)return*this;}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=gc();}if(ch=='.'){ch=gc();double cnt = 0.1;while(ch>='0'&&ch<='9')x=x+(ch-'0')*cnt,cnt/=10,ch=gc();}x*=sign;return*this;}
template<typename _Tp>
inline fastCin_v1_0_1&operator>>(_Tp&x){x=0;int sign=1;char ch=gc();if(ch==EOF)return*this;while(ch<'0'||ch>'9'){if(ch=='-')sign=-1;ch=gc();if(ch==EOF)return*this;}while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0',ch=gc();if(ch==EOF)return*this;}x*=sign;return*this;}
}fastCin;

封装究极快读快写

你还在因背不下来快读快写而烦恼吗?
你还在因突然发现代码需要快读快写,然而改起来很麻烦而烦恼吗?
你还在因为普通快读快写难以获得最优解而烦恼吗?
快使用我们的封装快读,用仿cin,cout的方式避免更改困难!!!
目前支持:整形(你能想到的都可以),浮点数(你能想到的都可以),string,c风格字符串……

食用方法:

  1. 有三对输入输出可以代替(cincout):(qins,qouts);(qinnqoutn);(qinfqoutf)。任选一种代替,不能混用它们!!!
  • qinsqouts)是慢速快读,可以用于 OJ 提交 & 本地调试 & 文件输入输出。
  • qinnqoutn)是普通快读,可以用于 OJ 提交 & 文件输入输出(本地调试极度困难,可能需要在文件最后反复输入 Ctrl+Z)。
  • qinfqoutf)是高速快读,可以用于 OJ 提交 & 文件输入输出(本地调试极度困难,可能需要在文件最后反复输入 Ctrl+Z)。
  1. 粘贴代码,更改您的 cincout(其实就是解除同步流,再次强调:不能混用它们)。

原理讲解:
快读的原理如出一辙,都是利用单个字符的输入。
慢速快读:使用 <cstdio> 库中的 std::getchar()std::putchar(),各自处理,各自输出。
普通快读:使用 <cstdio> 库中的 fread()fwrite(),先统一读入,各自处理,各自返回,再统一输出。
高速快读:使用 <iostream> 库中的 std::streambuf(),直接访问 cincout 缓存,快速读入读出。

namespace FastIOstream{bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}class instream{protected:bool iseof=false;virtual char getchar(){return EOF;}public:operator bool(){return!iseof;}template<class T>void read(T&x){double tmp=1;bool sign=0;x=0;char ch=getchar();if(ch==EOF||iseof){iseof=true;return;}for(;!isdigit(ch);ch=getchar())if(ch=='-')sign=1;for(;isdigit(ch);ch=getchar())x=x*10+(ch-'0');if(ch=='.')for(ch=getchar();isdigit(ch);ch=getchar())tmp/=10.0,x+=tmp*(ch-'0');if(sign)x=-x;}void read(char*s){char ch=getchar();if(ch==EOF||iseof){iseof=true;return;}for(;blank(ch);ch=getchar());for(;!blank(ch);ch=getchar())*s++=ch;*s=0;}void read(std::string s){char ch=getchar();if(ch==EOF||iseof){iseof=true;return;}for(;blank(ch);ch=getchar());for(;!blank(ch);ch=getchar())s.push_back(ch);}void read(char&c){c=getchar();if(c==EOF||iseof){iseof=true;return;}for(c=getchar();blank(c);c=getchar());}template<class T>friend instream&operator>>(instream&ins,T&in){ins.read(in);return ins;}};class fastinstream:public instream{protected:std::streambuf*cinbuf=std::cin.rdbuf();char inbuf[(1<<20)],*p1,*p2;char getchar(){if(p1==p2&&(p2=(p1=inbuf)+cinbuf->sgetn(inbuf,(1<<20)),p1==p2))return EOF;return*p1++;}public:void flush(){fflush(stdin);}fastinstream():p1(inbuf),p2(inbuf){}}qinf;class stdinstream:public instream{protected:char inbuf[(1<<20)],*p1,*p2;char getchar(){if(p1==p2)p2=(p1=inbuf)+fread(inbuf,1,(1<<20),stdin);return p1==p2?EOF:*p1++;}public:void flush(){fflush(stdin);}stdinstream():p1(inbuf),p2(inbuf){}}qinn;class errinstream:public instream{protected:char getchar(){return std::getchar();}}qins;struct ENDL{}endl;struct ENDS{}ends;struct FLUSH{}flush;class outstream{protected:virtual void putchar(const char&c){}public:virtual void flush(){}template<class T>void write(T x){if(x<0)x=-x,putchar('-');long long Z=x;long double R=x-Z;static int sta[35],top=0;do{sta[top++]=Z%10,Z/=10;}while(Z);while(top)putchar(sta[--top]+'0');if(R==0)return;putchar('.');R+=5e-11;for(int i=1;i<=10;i++){R*=10;putchar(int(R)+'0');R-=int(R);}}void write(char*s){for(int i=0;s[i];i++)putchar(s[i]);}void write(const char*s){for(int i=0;s[i];i++)putchar(s[i]);}void write(std::string s){for(int i=0;i<(int)s.size();i++)putchar(s[i]);}void write(char c){putchar(c);}void write(ENDL a){putchar('\n');flush();}void write(ENDS a){putchar(' ');flush();}void write(FLUSH a){flush();}template<class T>friend outstream&operator<<(outstream&outs,T out){outs.write(out);return outs;}};class fastoutstream:public outstream{protected:std::streambuf*coutbuf=std::cout.rdbuf();void putchar(const char&c){coutbuf->sputc(c);}public:void flush(){fflush(stdin);}}qoutf;class stdoutstream:public outstream{protected:char outbuf[(1<<20)],*op;void putchar(const char&c){if(op-outbuf==(1<<20))flush();*op++=c;}public:void flush(){fwrite(outbuf,1,op-outbuf,stdout);op=outbuf;}stdoutstream():op(outbuf){}~stdoutstream(){flush();}}qoutn;class erroutstream:public outstream{protected:void putchar(const char&c){std::putchar(c);}public:void flush(){fflush(stdout);}~erroutstream(){fflush(stdout);}}qouts;};using namespace FastIOstream;

搬运自某个初一的大佬的记事本