zoukankan      html  css  js  c++  java
  • 读入优化和输出优化模板

    ci<< scancin(rea<< fread

    c++入门:

    #include<bits/stdc++.h>
    
    using namespace std;
    int a;
    int main()
    {
        cin>>a;
        cout<<a<<endl;
        return 0;
    }
    View Code

    c里面的scanf

    #include<bits/stdc++.h>
    
    using namespace std;
    int a;
    int main()
    {
        scanf("%d",&a);
        printf("%d
    ",a);
        return 0;
    }
    View Code

    ios::sync_with_stdio(false):

    注意:关闭流同步后就不可以读入string类型了

    #include<bits/stdc++.h>
    
    using namespace std;
    int a;
    int main()
    {
        ios::sync_with_stdio(false);
        cin>>a;
        cout<<a<<endl;
        return 0;
    }
    View Code

    read的整数读入:

    #include<bits/stdc++.h>
    
    using namespace std;
    
    template <typename _Tp> inline _Tp read(_Tp&x){
        char c11=getchar(),ob=0;x=0;
        while(c11^'-'&&!isdigit(c11))c11=getchar();if(c11=='-')c11=getchar(),ob=1;
        while(isdigit(c11))x=x*10+c11-'0',c11=getchar();if(ob)x=-x;return x;
    }
    
    int main()
    {
        int a,b;
        read(a);read(b);
        cout<<a+b<<endl;
        return 0;
    }
    View Code

    read的浮点数读入:

    #include<bits/stdc++.h>
    
    using namespace std;
    
    inline bool read(double &num)
    {
            char in;double Dec=0.1;
            bool IsN=false,IsD=false;
            in=getchar();
            if(in==EOF) return false;
            while(in!='-'&&in!='.'&&(in<'0'||in>'9'))
                    in=getchar();
            if(in=='-'){IsN=true;num=0;}
            else if(in=='.'){IsD=true;num=0;}
            else num=in-'0';
            if(!IsD){
                    while(in=getchar(),in>='0'&&in<='9'){
                            num*=10;num+=in-'0';}
            }
            if(in!='.'){
                    if(IsN) num=-num;
                    return true;
            }else{
                    while(in=getchar(),in>='0'&&in<='9'){
                            num+=Dec*(in-'0');Dec*=0.1;
                    }
            }
            if(IsN) num=-num;
            return true;
    }
    
    int main()
    {
        double a,b;
        read(a);read(b);
        cout<<a+b<<endl;
        return 0;
    }
    View Code

    更加快速的fread

    注意:查看结果需要文件

    #include<bits/stdc++.h>
    
    using namespace std;
    
    struct ios {
        inline char read(){
            static const int IN_LEN=1<<18|1;
            static char buf[IN_LEN],*s,*t;
            return (s==t)&&(t=(s=buf)+fread(buf,1,IN_LEN,stdin)),s==t?-1:*s++;
        }
    
        template <typename _Tp> inline ios & operator >> (_Tp&x){
            static char c11,boo;
            for(c11=read(),boo=0;!isdigit(c11);c11=read()){
                if(c11==-1)return *this;
                boo|=c11=='-';
            }
            for(x=0;isdigit(c11);c11=read())x=x*10+(c11^'0');
            boo&&(x=-x);
            return *this;
        }
    } io;
    
    int main()
    {
        int a,b;
        io>>a>>b;
        cout<<a+b<<endl;
        return 0;
    }
    View Code

    快速输出  putchar

    整数:

    #include<bits/stdc++.h>
    
    using namespace std;
    
    template <typename T> void printe(const T x)
    {
        if(x>=10)printe(x/10);
        putchar(x%10+'0');
    }
    template <typename T> inline void print(const T x)
    {
        if(x<0)putchar('-'),printe(-x);
        else printe(x);
    }
    
    int main()
    {
        int a,b;
        cin>>a>>b;
        print(a+b);
        return 0;
    }
    View Code

    总结:

    快速读入的原理是基于 cin << scanf << getchar()/putschar(); 从而利用getchar()/putchar()进行加速的。

  • 相关阅读:
    es6 学习笔记
    正则表达式 学习笔记
    knockout 学习笔记
    错误笔记
    zepto 学习笔记
    Dialog提示框
    setContentView( )方法
    SVG图形之几何图形——学习笔记2
    SVG图形之SVG文件如何嵌入HTML文档——学习笔记1
    getchar()的用法
  • 原文地址:https://www.cnblogs.com/solvit/p/9501372.html
Copyright © 2011-2022 走看看