c++ 快速读入输出

1. 读入优化

  • C++中有一个函数:getchar() ,用于读入字符,那么这跟读入整数有什么关系呢?

  • 其实,经过类似高精度的处理,就可以实现类型转换啦!

  • 下面是正负数读入优化模板:

  • #include<cctype>
    inline int read()
    {
        int X=0,w=0; char ch=0;
        while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
        while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
        return w?-X:X;
    }

    isdigit(x)表示 xx 是否是 0 ~ 9 的整数 ,是则返回 truetrue ,不是则是 falsefalse 。

    注意要用 cctype 头文件。

    像这样“X=read()”使用函数即可,效率超高!

    这里补充一个正负实数的读入优化:

    inline double dbread()
    {
        double X=0,Y=1.0; int w=0; char ch=0;
        while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
        while(isdigit(ch)) X=X*10+(ch^48),ch=getchar();
        ch=getchar();//读入小数点
        while(isdigit(ch)) X+=(Y/=10)*(ch^48),ch=getchar();
        return w?-X:X;
    }
  • 还有一个版本
  • int read(){
        int x=0;bool f=0;char c=getchar();
        while (c<'0'||c>'9'){if (c=='-')f=1;c=getchar();}
        while (c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
        return f?-x:x;
    }
  • 附fread:

    char buffer[100001],*S,*T; 
    inline char Get_Char()  {  
        if (S==T){  
            T=(S=buffer)+fread(buffer,1,100001,stdin);
            if (S==T) return EOF;  
        }
        return *S++; 
    }
    inline int read(){
        char c;int re=0;  
        for(c=Get_Char();c<'0'||c>'9';c=Get_Char());  
        while(c>='0'&&c<='9') re=(re<<1)+(re<<3)+(c-'0'),c=Get_Char();  
        return re;  
    }
  • struct IO_Tp
    {
        static const int _I_Buffer_Size = 1 << 24;
        char _I_Buffer[_I_Buffer_Size];
        char* _I_pos;
        static const int _O_Buffer_Size = 1 << 24;
        char _O_Buffer[_O_Buffer_Size];
        char* _O_pos;
        IO_Tp() : _I_pos(_I_Buffer), _O_pos(_O_Buffer)
        {
            fread(_I_Buffer, 1, _I_Buffer_Size, stdin);
        }
        ~IO_Tp()
        {
            fwrite(_O_Buffer, 1, _O_pos - _O_Buffer, stdout);
        }
        inline bool is_digit(const char ch)
        {
            return '0' <= ch && ch <= '9';
        }
        inline IO_Tp& operator>>(int& res)
        {
            res = 0;
            while (!is_digit(*_I_pos))
                ++_I_pos;
            do
                (res *= 10) += (*_I_pos++) & 15;
            while (is_digit(*_I_pos));
            return *this;
        }
        inline IO_Tp& operator<<(int n)
        {
            static char _buf[10];
            char* _pos(_buf);
            do
                *_pos++ = '0' + n % 10;
            while (n /= 10);
            while (_pos != _buf)
                *_O_pos++ = *--_pos;
            return *this;
        }
        inline IO_Tp& operator<<(char ch)
        {
            *_O_pos++ = ch;
            return *this;
        }
    } IO;
  • 2. 输出优化

    • 在个别繁杂题目中,也可以使用输出优化!

    • 与读入优化类似,使用用来输出字符的“putchar()”函数,也可到目的

    • 下面是正负数输出优化模板:

    • inline void write(int x)
      {
           if(x<0) putchar('-'),x=-x;
           if(x>9) write(x/10);
           putchar(x%10+'0');
      }
      • 使用读入优化与输出优化,可以在一些题目中赢得很多时间,甚至逆转AC!

      • 而且代码很短,性价比很高,完全可以经常使用!

      注意

      • 切记在使用时,应注意数据类型大小和正负!