c++快读快写
c++快读快写
cin
换成in
,cout
换成out
,cerr
换为err
- 输出换行:
out<<endln
err<<endln
- 输出清空缓存(flush):
out<<flush
err<<flush
(不手动清理则在程序结束时自动清理) - get line:
string s;in.getline(s);
或char s[10000];getline(s);
- 支持
int128
,支持while(in>>x)
和while(in.getline(s))
- 输出设置小数精度
out<<precision(6)
(默认:$6$) - 若小数位不足是否补$0$
out<<force(false)
(默认:$false$) - 小于精度的部分是否四舍五入(否则截断)
out<<round(true)
(默认:$true$) - 整数长度不足时添加前导$0$
out<<lead(1)
(默认:$1$)
默认缓存区大小为
in
out
各1MB
($2^{20}$字节)err
缓存为$1$字节修改缓存大小: 例如将
out
的缓存设置为$2^{10}$字节,将$179$行的out(stdout)
改为out(stdout,1<<10)
程序异常退出($RE$)可能导致
out
的缓存没有清空(没被输出),将缓存区设为$1$字节可避免这一情况,但会导致效率下降.(若希望提高err
的效率或避免out
缓存没被清空,可修改它们的缓存大小)
getchar()
,putchar(c)
,puts(s)
现在应该使用in.getc()
,out.putc(c)
,out<<s
否则可能导致输出错乱文件输入输出可以使用
freopen
重定向stdio
也可以创建新的对象,例:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 struct Out out(stdout),err(stderr,1); struct In in(stdin); struct Out fout0("out0.out"); //创建文件输出,输出到"out0.out"缓存为2^20字节 struct Out fout1("out1.out",1024);//创建文件输出,输出到"out1.out"缓存为1024字节 struct In fin0("in0.in");//创建文件输入,从"in0.in"读入,缓存2^20字节 struct In fin1("in1.in",1024);//创建文件输入,从"in1.in"读入,缓存1024字节 string x; in>>x;//标准输入 out<<x;//标准输出 err<<x;//标准错误输出 fin0>>x;//从"in0.in"输入 fout0<<x;//输出到"out0.out"
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <bits/stdc++.h>
using namespace std;
struct Out
{
private:
FILE*file;
char*buf,*p1,*pend;
int precision=6;
bool force=false;
bool round=true;
int wight=0;
public:
explicit Out(FILE*file,int size=1<<20):file(file),buf(new char[size]),p1(buf),pend(buf+size){}
explicit Out(const string&file,int size=1<<20):Out(fopen(file.c_str(),"w"),size){}
Out(const Out&)=delete;
Out&operator=(const Out&)=delete;
Out(Out&&)=delete;
Out&operator=(Out&&)=delete;
void setPrecision(int x){precision=x;}
void setForce(bool x){force=x;}
void setRound(bool x){round=x;}
void setLead(int x){wight=x;}
~Out(){flush();delete[] buf;}
void flush(){fwrite(buf,1,p1-buf,file),p1=buf;}
void putc(char c){*p1++=c;if (p1==pend)flush();}
#define retp(x) template<class T> typename enable_if<x,Out>::type
#define lmt(x) x<T>::value
retp(lmt(is_unsigned)&&lmt(is_integral))&operator<<(T x)
{
static char s[50];
int len=0;
do
{
s[len++]=x%10+'0';
x/=10;
} while (x||len<wight);
while (len--)putc(s[len]);
return *this;
}
retp(lmt(is_signed)&&lmt(is_integral))&operator<<(T x)
{
make_unsigned_t<T> y=x;
if (x<0)putc('-'),y=-y;
*this<<y;
return *this;
}
retp(lmt(is_floating_point))&operator<<(T x)
{
if (x!=x)putc('N'),putc('a'),putc('N');
else if (x==numeric_limits<T>::infinity())putc('I'),putc('n'),putc('f');
else if (x==-numeric_limits<T>::infinity())putc('-'),putc('I'),putc('n'),putc('f');
else
{
if (x<0)putc('-'),x=-x;
if (round)x+=0.5*pow(0.1,precision);
auto t=static_cast<intmax_t>(x);
*this<<t;
x-=t;
x*=pow(10,precision);
t=static_cast<intmax_t>(x);
while (t%10==0&&t&&!force)t/=10;
if (t||force)putc('.'),*this<<t;
}
return *this;
}
#undef retp
#undef lmt
Out&operator<<(const char*s){while (*s)putc(*s++);return *this;}
Out&operator<<(const string&s){for (auto c:s)putc(c);return *this;}
Out&operator<<(const char&c){putc(c);return *this;}
Out&operator<<(const function<void(Out&)>&f){f(*this);return *this;}
};
const function<void(Out&)> endln=[](Out&o){o.putc('\n');};
const function<void(Out&)> flush=[](Out&o){o.flush();};
function<void(Out&)> precision(int n){return [n](Out&o){o.setPrecision(n);};}
function<void(Out&)> force(bool b){return [b](Out&o){o.setForce(b);};}
function<void(Out&)> round(bool b){return [b](Out&o){o.setRound(b);};}
function<void(Out&)> lead(int n){return [n](Out&o){o.setLead(n);};}
struct In
{
private:
FILE*file;
char*buf,*p1,*pend;
bool eof;
bool rd=true;
void read(){p1=buf;pend=buf+fread(buf,1,pend-buf,file);eof=pend==p1;}
public:
explicit In(FILE*file,int size=1<<20):file(file),buf(new char[size]),p1(buf+size),pend(buf+size),eof(false){}
explicit In(const string&file,int size=1<<20):In(fopen(file.c_str(),"r"),size){}
In(const In&)=delete;
In&operator=(const In&)=delete;
In(In&&)=delete;
In&operator=(In&&)=delete;
~In(){delete[] buf;}
char getc(){if (p1==pend)read();return eof?(char)-1:*p1++;}
char peek(){if (p1==pend)read();return eof?(char)-1:*p1;}
explicit operator bool ()const{return rd;}
#define retp(x) template<class T> typename enable_if<x,In>::type
#define lmt(x) x<T>::value
retp(lmt(is_unsigned)&&lmt(is_integral))&operator>>(T&x)
{
rd=false;
x=0;
char c;
while (!isdigit(c=getc())&&c!=-1);
for (;isdigit(c);c=getc())x=x*10+c-'0',rd=true;
return *this;
}
retp(lmt(is_signed)&&lmt(is_integral))&operator>>(T&x)
{
rd=false;
x=0;
char c;
while (!isdigit(c=getc())&&c!='-'&&c!=-1);
bool neg=false;
if (c=='-')neg=true,c=getc();
for (;isdigit(c);c=getc())x=x*10+(neg?'0'-c:c-'0'),rd=true;
return *this;
}
retp(lmt(is_floating_point))&operator>>(T&x)
{
rd=false;
x=0;
char c;
while (!isdigit(c=getc())&&c!='-'&&c!=-1);
bool neg=false;
if (c=='-')neg=true,c=getc();
for (;isdigit(c);c=getc())x=x*10+(neg?'0'-c:c-'0'),rd=true;
if (c=='.')
{
c=getc();
T y=0;
int cnt=0;
for (;isdigit(c);c=getc())y=y*10+(neg?'0'-c:c-'0'),cnt++;
x+=y*pow(0.1,cnt);
}
return *this;
}
#undef retp
#undef lmt
In&operator>>(char *s)
{
rd=false;
while (isspace(*s=getc())&&*s!=-1);
while (!isspace(*++s=getc())&&*s!=-1)rd=true;
*s=0;
return *this;
}
In&operator>>(string&s)
{
rd=false;
s.clear();
char c;
while (isspace(c=getc())&&c!=-1);
for (;!isspace(c)&&c!=-1;c=getc())s+=c,rd=true;
return *this;
}
In&operator>>(char&c)
{
while (isspace(c=getc())&&c!=-1){}
rd=c!=-1;
return *this;
}
In&getline(char*s)
{
while ((*s=getc())=='\n'&&*s!=-1){}
while ((*++s=getc())!='\n'&&*s!=-1)rd=true;
return *s=0,*this;
}
In&getline(string&s)
{
s.clear();
char c;
while ((c=getc())=='\n'&&c!=-1){}
for (;c!='\n'&&c!=-1;c=getc())s+=c,rd=true;
return *this;
}
};
struct Out out(stdout),err(stderr,1);
struct In in(stdin);
本文由作者按照 CC BY-NC 4.0 进行授权, 未经许可不得转载。