C++實用整數快速輸入輸出模板(C++)


隨便寫一點放在這里,以后想蛇皮卡常就很方便啦
蒟蒻太懶了,也就暫時不搞什么封namespace之類的操作了
程序結束時記得flush一下。

#include<cstdio>
#define RG register
#define IV inline void
#define TP template<typename T>
#define gc          if(++pi==iend)fread (pi=ibuf,1,SZ,stdin)
#define pc(C) *po=C;if(++po==oend)fwrite(po=obuf,1,SZ,stdout)
const int SZ=1<<20;
char ibuf[SZ],obuf[SZ],*pi=ibuf+SZ-1,*po=obuf;
const char*iend=ibuf+SZ,*oend=obuf+SZ;
TP IV in(RG T&x){
	gc;while(*pi<'-')gc;
	x=*pi&15;gc;
	while(*pi>'-'){x*=10;x+=*pi&15;gc;}
}
TP IV iex(RG T&x){
	gc;while(*pi<'-')gc;
	RG bool f=*pi=='-';if(f)gc;
	x=*pi&15;gc;
	while(*pi>'-'){x*=10;x+=*pi&15;gc;}
	if(f)x=-x;
}
TP IV out(RG T x){
	if(x>9)out(x/10);
	pc(x%10|'0');
}
TP IV oex(RG T x){
	if(x<0){pc('-');x=-x;}
	out(x);
}
int main(){
	//do something
	fwrite(obuf,1,po-obuf,stdout);
	return 0;
}

Tips:

  1. gc只是把指針移動到下一個字符,獲得字符直接*pi
  2. 不帶ex的只資磁非負整數
  3. out使用了函數堆棧,在數比較小時快,int差不多,longlong級別建議改寫成手工字符堆棧

update

附上蒟蒻現在已經敲熟了的gi,非常簡單,可以直接應用在輸入數據中只有整數的考試題目中,直接替代getchar()式的快讀,並不容易寫掛。

#define gc if(++ip==ie)fread(ip=buf,1,SZ,stdin)
const int SZ=1<<19;
char buf[SZ],*ie=buf+SZ,*ip=ie-1;
inline int gi(){
    gc;while(*ip<'-')gc;
    bool f=*ip=='-';if(f)gc;
    int x=*ip&15;gc;
    while(*ip>'-'){x*=10;x+=*ip&15;gc;}
    return f?-x:x;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM