输入输出优化

 1 #include<bits/stdc++.h>
 2 const int RN=1e5;
 3 char ib[RN+5],*ip=ib+RN,ob[RN+1007],*op=ob;
 4 inline int gc(){
 5     ip==ib+RN?fread(ip=ib,1,RN,stdin)[ib]=0:0;
 6     return*ip++;
 7 }
 8 struct IO{
 9     void flush(){
10         fwrite(ob,1,op-ob,stdout),op=ob;
11     }
12     template<class T>//T=int,long long
13     IO&operator>>(T&x0){
14         T x=0;
15         int f=1;
16         if(ip<ib+RN-100){
17             while(*ip<48)*ip++=='-'?f=-1:0;
18             while(*ip>47)x=x*10+*ip++-48;
19         }else{
20             int c=gc();
21             while(c<48)c=='-'?f=-1:0;
22             while(c>47)x=x*10+c-48,c=gc();
23         }
24         x0=x;
25         return *this;
26     }
27     IO&operator>>(char*s){
28         int c=gc();
29         while(c<33)c=gc();
30         while(c>32)*s++=c,c=gc();
31         *s=0;
32         return *this;
33     }
34     template<class T>//T=int,long long,char
35     IO&operator<<(T x){
36         if(op>ob+RN)flush();
37         int ss[25],sp=0;
38         if(x<0)*op++='-',x=-x;
39         do ss[++sp]=48+x%10;while(x/=10);
40         while(sp)*op++=ss[sp--];
41         return *this;
42     }
43     IO&operator<<(char x){
44         if(op>ob+RN)flush();
45         *op++=x;
46         return *this;
47     }
48     IO&operator<<(const char*str){
49         int n=strlen(str);
50         if(op+n>ob+RN)flush();
51         if(n<RN)memcpy(op,str,n),op+=n;
52         else fwrite(str,1,n,stdout);
53         return *this;
54     }
55     IO&operator<<(char*str){
56         return *this<<(const char*)str;
57     }
58     IO&operator<<(std::string str){
59         return *this<<str.data();
60     }
61 }io;
62 int main(){
63     freopen("in.txt","r",stdin);
64     int a,b;
65     io>>a>>b;
66     io<<a<<'+'<<b<<'='<<a+b<<'
';
67     io<<"test_string_output
";
68     char s[111];
69     io>>s;
70     io<<s<<'
';
71     io.flush();
72     freopen("o.txt","w",stdout);
73     for(int i=0;i<10000000;++i)io<<i<<' ';
74     io.flush();
75     return 0;
76 }
View Code
原文地址:https://www.cnblogs.com/mczhuang/p/7647969.html