にゅうしゅつりょくさいてきか
7562 ワード
にゅうしゅつりょくさいてきか
(C++)ライブラリには多くの入力と出力方式があり、私たちが最もよく使うのは(scanf),(printf)と(cin),(cout)です.このほかにも、(getchar)(putchar),(gets),(puts),(fgets),(fputs),(getline)などが一般的です.入力または出力のデータ量が大きすぎる場合、より効率的な入力と出力方式を選択すると、プログラムの実行に多くの時間を節約できます.
きほんげんり
一般に、入力の効率については、(cin<こうりつぶんせき
ほとんどの場合、通常の入出力最適化は問題を解決するのに十分です.しかし,依然としてごく少数の問題では,通常の入出力最適化が詰まっている.この場合、(getchar),(putchar)よりも速い(fread),(fwrite)を使用する必要があります.
コアコード
簡易版
整数の入出力最適化.
通常版
整数、浮動小数点数、文字、文字配列、文字列の入出力最適化.
強化版
(fread),(fwrite)に基づく入出力最適化.なお、この最適化を使用すると、ファイルの最後に読み込まないと入力が停止しません.キーボードを使用してデータを入力する場合は、手動で(EOF)を追加する必要があります((Windows)システムの下で、(EOF)手動入力は(Ctrl+z)を押します.(Ubuntu)システムの下で、(EOF)手動入力は(Ctrl+d)を押します.
(C++)ライブラリには多くの入力と出力方式があり、私たちが最もよく使うのは(scanf),(printf)と(cin),(cout)です.このほかにも、(getchar)(putchar),(gets),(puts),(fgets),(fputs),(getline)などが一般的です.入力または出力のデータ量が大きすぎる場合、より効率的な入力と出力方式を選択すると、プログラムの実行に多くの時間を節約できます.
きほんげんり
一般に、入力の効率については、(cin<
ほとんどの場合、通常の入出力最適化は問題を解決するのに十分です.しかし,依然としてごく少数の問題では,通常の入出力最適化が詰まっている.この場合、(getchar),(putchar)よりも速い(fread),(fwrite)を使用する必要があります.
コアコード
簡易版
整数の入出力最適化.
templateinline bool read(T &x)
{
x=0;register char c=getchar();register bool f=0;
while(!isdigit(c)){if(c==EOF)return false;f^=c=='-',c=getchar();}
while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
if(f)x=-x;
return true;
}
templateinline void print(T x)
{
if(x<0)putchar('-'),x=-x;
if(x>9)print(x/10);
putchar(x%10^48);
}
templateinline void print(T x,char c){print(x),putchar(c);}
通常版
整数、浮動小数点数、文字、文字配列、文字列の入出力最適化.
templateinline bool read(T &x)
{
x=0;register char c=getchar();register bool f=0;
while(!isdigit(c)){if(c==EOF)return false;f^=c=='-',c=getchar();}
while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
if(f)x=-x;
return true;
}
templateinline bool readd(T &x)
{
register ll X=0;register double y=1.0;register char c=getchar();register bool f=0;
while(!isdigit(c)){if(c==EOF)return false;f^=c=='-',c=getchar();}
while(isdigit(c))X=(X<<3)+(X<<1)+(c^48),c=getchar();
x=X;
if(c!='.')return true;
c=getchar();
while(isdigit(c))x+=(y/=10)*(c^48),c=getchar();
if(f)x=-x;
return true;
}
templateinline bool readc(T &x)
{
register char c=getchar();
while(c==' '||c=='
'||c=='\r'||c=='\t')c=getchar();
if(c==EOF)return false;
x=c;
return true;
}
templateinline bool readc(T *x)
{
register char c=getchar();
while(c==' '||c=='
'||c=='\r'||c=='\t')c=getchar();
if(c==EOF)return false;
while(c!=' '&&c!='
'&&c!='\r'&&c!='\t'&&c!=EOF)*x++=c,c=getchar();
*x=0;
return true;
}
templateinline bool reads(T &x)
{
x="";register char c=getchar();
while(c==' '||c=='
'||c=='\r'||c=='\t')c=getchar();
if(c==EOF)return false;
while(c!=' '&&c!='
'&&c!='\r'&&c!='\t'&&c!=EOF)x+=c,c=getchar();
return true;
}
templateinline void print(T x)
{
if(x<0)putchar('-'),x=-x;
if(x>9)print(x/10);
putchar(x%10^48);
}
templateinline void printd(T x,ll y)
{
static ll mul[]={1};
for(register ll i=1;i<=18;i++)
mul[i]=(mul[i-1]<<3)+(mul[i-1]<<1);
if(x0)
{
putchar('.');
for(register ll i=1;iinline void printc(T x){putchar(x);}
templateinline void printc(T *x){while(*x)putchar(*x++);}
templateinline void prints(T x){for(register ll i=0;x[i]!='\0';i++)putchar(x[i]);}
templateinline void print(T x,char c){print(x),putchar(c);}
templateinline void printd(T x,ll y,char c){printd(x,y),putchar(c);}
templateinline void printc(T x,T c){printc(x),putchar(c);}
templateinline void printc(T *x,T c){printc(x),putchar(c);}
templateinline void prints(T x,char c){prints(x),putchar(c);}
強化版
(fread),(fwrite)に基づく入出力最適化.なお、この最適化を使用すると、ファイルの最後に読み込まないと入力が停止しません.キーボードを使用してデータを入力する場合は、手動で(EOF)を追加する必要があります((Windows)システムの下で、(EOF)手動入力は(Ctrl+z)を押します.(Ubuntu)システムの下で、(EOF)手動入力は(Ctrl+d)を押します.
#define getchar gc
#define putchar pc
inline char gc()
{
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
static char buf[100000],*pp=buf;
templateinline void pc(T c)
{
if(pp-buf==100000)fwrite(buf,1,100000,stdout),pp=buf;
*pp++=c;
}
inline void fsh(){fwrite(buf,1,pp-buf,stdout);pp=buf;}
templateinline bool read(T &x)
{
x=0;register char c=getchar();register bool f=0;
while(!isdigit(c)){if(c==EOF)return false;f^=c=='-',c=getchar();}
while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
if(f)x=-x;
return true;
}
templateinline bool readd(T &x)
{
register ll X=0;register double y=1.0;register char c=getchar();register bool f=0;
while(!isdigit(c)){if(c==EOF)return false;f^=c=='-',c=getchar();}
while(isdigit(c))X=(X<<3)+(X<<1)+(c^48),c=getchar();
x=X;
if(c!='.')return true;
c=getchar();
while(isdigit(c))x+=(y/=10)*(c^48),c=getchar();
if(f)x=-x;
return true;
}
templateinline bool readc(T &x)
{
register char c=getchar();
while(c==' '||c=='
'||c=='\r'||c=='\t')c=getchar();
if(c==EOF)return false;
x=c;
return true;
}
templateinline bool readc(T *x)
{
register char c=getchar();
while(c==' '||c=='
'||c=='\r'||c=='\t')c=getchar();
if(c==EOF)return false;
while(c!=' '&&c!='
'&&c!='\r'&&c!='\t'&&c!=EOF)*x++=c,c=getchar();
*x=0;
return true;
}
templateinline bool reads(T &x)
{
x="";register char c=getchar();
while(c==' '||c=='
'||c=='\r'||c=='\t')c=getchar();
if(c==EOF)return false;
while(c!=' '&&c!='
'&&c!='\r'&&c!='\t'&&c!=EOF)x+=c,c=getchar();
return true;
}
templateinline void print(T x)
{
if(x<0)putchar('-'),x=-x;
if(x>9)print(x/10);
putchar(x%10^48);
}
templateinline void printd(T x,ll y)
{
static ll mul[]={1};
for(register ll i=1;i<=18;i++)
mul[i]=(mul[i-1]<<3)+(mul[i-1]<<1);
if(x0)
{
putchar('.');
for(register ll i=1;iinline void printc(T x){putchar(x);}
templateinline void printc(T *x){while(*x)putchar(*x++);}
templateinline void prints(T x){for(register ll i=0;x[i]!='\0';i++)putchar(x[i]);}
templateinline void print(T x,char c){print(x),putchar(c);}
templateinline void printd(T x,ll y,char c){printd(x,y),putchar(c);}
templateinline void printc(T x,T c){printc(x),putchar(c);}
templateinline void printc(T *x,T c){printc(x),putchar(c);}
templateinline void prints(T x,char c){prints(x),putchar(c);}
int main()
{
return fsh(),0;
}