输出读入优化——QAQ

#include<bits/stdc++.h>
const int RN=1e5;
char ib[RN+5],*ip=ib+RN,ob[RN+1007],*op=ob;
inline int gc(){
    ip==ib+RN?fread(ip=ib,1,RN,stdin)[ib]=0:0;
    return*ip++;
}
struct IO{
    void flush(){
        fwrite(ob,1,op-ob,stdout),op=ob;
    }
    template<class T>//T=int,long long
    IO&operator>>(T&x0){
        T x=0;
        int f=1;
        if(ip<ib+RN-100){
            while(*ip<48)*ip++==‘-‘?f=-1:0;
            while(*ip>47)x=x*10+*ip++-48;
        }else{
            int c=gc();
            while(c<48)c==‘-‘?f=-1:0;
            while(c>47)x=x*10+c-48,c=gc();
        }
        x0=x;
        return *this;
    }
    IO&operator>>(char*s){
        int c=gc();
        while(c<33)c=gc();
        while(c>32)*s++=c,c=gc();
        *s=0;
        return *this;
    }
    template<class T>//T=int,long long,char
    IO&operator<<(T x){
        if(op>ob+RN)flush();
        int ss[25],sp=0;
        if(x<0)*op++=‘-‘,x=-x;
        do ss[++sp]=48+x%10;while(x/=10);
        while(sp)*op++=ss[sp--];
        return *this;
    }
    IO&operator<<(char x){
        if(op>ob+RN)flush();
        *op++=x;
        return *this;
    }
    IO&operator<<(const char*str){
        int n=strlen(str);
        if(op+n>ob+RN)flush();
        if(n<RN)memcpy(op,str,n),op+=n;
        else fwrite(str,1,n,stdout);
        return *this;
    }
    IO&operator<<(char*str){
        return *this<<(const char*)str;
    }
    IO&operator<<(std::string str){
        return *this<<str.data();
    }
}io;
int main(){
    freopen("in.txt","r",stdin);
    int a,b;
    io>>a>>b;
    io<<a<<‘+‘<<b<<‘=‘<<a+b<<‘\n‘;
    io<<"test_string_output\n";
    char s[111];
    io>>s;
    io<<s<<‘\n‘;
    io.flush();
    freopen("o.txt","w",stdout);
    for(int i=0;i<10000000;++i)io<<i<<‘ ‘;
    io.flush();
    return 0;
}

时间: 2024-08-28 14:57:44

输出读入优化——QAQ的相关文章

【墙裂推荐】读入优化和输出优化

读入优化: 1 inline int read() 2 { 3 int X=0,w=1; char ch=0; 4 while(ch<'0' || ch>'9') {if(ch=='-') w=-1;ch=getchar();} 5 while(ch>='0' && ch<='9') X=(X<<3)+(X<<1)+ch-'0',ch=getchar(); 6 return X*w; 7 } 输出优化: 1 inline void write

读入优化 &amp;&amp; 输出优化

qwq算是一个板子随笔吧 快读我在某大佬的博客中找到了更短的代码 但是这个写习惯了就改不了了qwq 其实是我不想改 废话好多 直接贴代码 1 //读入优化 2 inline int read(){ 3 char ch, c; 4 int res; 5 while (ch = getchar(), ch < '0' || ch > '9') c = ch; 6 res = ch - 48; 7 while (ch = getchar(), ch >= '0' && ch &

读入优化&amp;输出优化

读入优化:读入优化只是针对整数,由于getchar()读字符非常的快,所以采用getchar()来进行读入,下设输入的数为x 负数处理:用一个标志变量f,开始时为1,当读入了'-'时,f变为-1,最后x*=f即可 绝对值部分处理:getchar()每次只能读一位,所以每当读了一位s时,x*=10,为s留位置,由于s为字符,需要减去'0'才能转为整数即:x=x*10+s-'0' 关于细节: 很多时候有多余的空格或者其它的一些无关字符输入,处理时需要注意排除 代码: 1 void read(int

c++ 读入优化、输出优化模板

0. 在有些输入数据很多的变态题中,scanf会大大拖慢程序的时间,cin就更慢了,所以就出现了读入优化.其原理就是一个一个字符的读入,输出优化同理,主要使用getchar,putchar函数. 1. int型读入优化(long long的只要把x改成long long型即可): 1 #include<cctype> 2 inline int read() 3 { 4 int x=0,f=0; char ch=0; 5 while(!isdigit(ch)) {f|=ch=='-';ch=ge

奇技淫巧:NOIP的读入优化

最近看到洛谷上面有一个读入优化的代码: inline char get_char(){//劲者快读 static char buf[1000001],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++; } inline short read(){ short num=0; char c; while(isspace(c=get_char())); while(n

【日常学习】【读入优化】codevs2555 a+b=?题解

题目很简单 题目描述 Description 做了,简单的a,b和的问题.但是,如果要求输入的情况不是a和b,而是整个加法表达式呢? 请想办法,计算加法表达式的结果. 输入描述 Input Description 输入一个加法表达式,如1+2=. 输出描述 Output Description 计算出输入表达式的正确结果 样例输入 Sample Input 1+2= 样例输出 Sample Output 3 数据范围及提示 Data Size & Hint 完整的输入表达式.加号两边的数值属于-

关于读入优化的最终分析

关于读入优化的最终分析 摘要 身为一只以卡常为生的蒟蒻,总想着通过一些奇技淫巧来掩饰优化常数. 于是本文章就非正式地从最初的开始到最终的终止来谈谈在OI种各种主流.非主流读入的速度以及利弊. 序言 随着算法的发展各种数据结构等劲题出现,这些题除了思维难度的提高还带来者输入数据的增多(特别的有:uoj.ac上的一道题需要选手自己生成数据,而数论题往往输入较少),对于有追求有理想的选手快速读入是必不可少的技能. 尽管市面上有不同的主流读入优化,但是大多都是基于fread的,其余的只是一些小变动. 而

bzoj3685: 普通van Emde Boas树 set+读入优化

显然这题的所有操作都可以用set,但是直接用set肯定要T,考虑到读入量较大,使用fread读入优化,就可以卡过去了. #include<bits/stdc++.h> using namespace std; void read(int& x){ const int k=1600000; static char v, u[k],*s=u,*t=u; x=0; while(isspace(v=s==t &&u==(t=u+fread(s=u, 1,k,stdin))?-1

OI中整数的读入优化

将整数一个字符一个字符地读入,再转成整数,比直接作为整数读入快. 在读入大规模的整数数据时比较有效. 代码如下: inline void read_int(int &num) { char c; while (c = getchar(), c < '0' || c > '9'); num = c - '0'; while (c = getchar(), c >= '0' && c <= '9') num = num * 10 + c - '0'; } OI中