zoukankan      html  css  js  c++  java
  • C++快速输入输出优化

    在这里存一下我的快速输入输出优化

    以及写题模板

    这里的是$getchar$优化和$putchar$优化,$fread$和$fwrite$暂时咕咕咕

    fread已补

    快速输入

    这里$define$了一个$I\_int$,改读入的数据类型的话直接在$define$那里改就好

    #define I_int int 
    inline I_int read() {
           I_int x = 0 , f = 1 ; char c = getchar() ;
        while( c < '0' || c > '9' ) { if( c == '-' ) f = -1 ; c = getchar() ; } 
        while( c >= '0' && c <= '9' ) { x = x * 10 + c - '0' ; c = getchar() ; } 
        return x * f ;
    } 
    #undef I_int

    快速输出

    同上

    #define I_int int 
    char F[ 200 ] ;
    inline void write( I_int x ) {
        I_int tmp = x > 0 ? x : -x ;
        if( x < 0 ) putchar( '-' ) ;
        int cnt = 0 ;
           while( tmp > 0 ) {
               F[ cnt ++ ] = tmp % 10 + '0' ;
               tmp /= 10 ;
           }
           while( cnt > 0 ) putchar( F[ -- cnt ] ) ;
    }
    #undef I_int

    整个的io优化板子

    封装到了一个$namespace$里面,写题的时候可以收起来看着会比较舒服QAQ

    namespace io {
    
        #define in(a) a=read()
        #define out(a) write(a)
        #define outn(a) out(a),putchar('
    ')
    
        #define I_int int 
        inline I_int read() {
            I_int x = 0 , f = 1 ; char c = getchar() ;
            while( c < '0' || c > '9' ) { if( c == '-' ) f = -1 ; c = getchar() ; } 
            while( c >= '0' && c <= '9' ) { x = x * 10 + c - '0' ; c = getchar() ; } 
            return x * f ;
        } 
        char F[ 200 ] ;
        inline void write( I_int x ) {
            if( x == 0 ) { putchar( '0' ) ; return ; }
            I_int tmp = x > 0 ? x : -x ;
            if( x < 0 ) putchar( '-' ) ;
            int cnt = 0 ;
            while( tmp > 0 ) {
                F[ cnt ++ ] = tmp % 10 + '0' ;
                tmp /= 10 ;
            }
            while( cnt > 0 ) putchar( F[ -- cnt ] ) ;
        }
        #undef I_int
    
    }
    using namespace io ;

    fread读入优化

    就是把原来那个$read$的$getchar$换成了$fread$,这样会更快

    但是一个不好的地方是必须读文件流。不能用cmd.

    char buf[1<<21], *p1 = buf, *p2 = buf;
    inline char gc() {
        if(p1 != p2) return *p1++;
        p1 = buf;
        p2 = p1 + fread(buf, 1, 1 << 21, stdin);
        return p1 == p2 ? EOF : *p1++;
    }
    #define G gc
    
    #ifndef ONLINE_JUDGE
    #undef G
    #define G getchar
    #endif
    
    template<class I>
    inline void read(I &x) {
        x = 0; I f = 1; char c = G();
        while(c < '0' || c > '9') {if(c == '-') f = -1; c = G(); }
        while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = G(); }
        x *= f;
    }

    写题模板

    #include <bits/stdc++.h>
    using namespace std;
    
    namespace io {
    char buf[1<<21], *p1 = buf, *p2 = buf, buf1[1<<21];
    inline char gc() {
        if(p1 != p2) return *p1++;
        p1 = buf;
        p2 = p1 + fread(buf, 1, 1 << 21, stdin);
        return p1 == p2 ? EOF : *p1++;
    }
    #define G gc
    
    #ifndef ONLINE_JUDGE
    #undef G
    #define G getchar
    #endif
    
    template<class I>
    inline void read(I &x) {
        x = 0; I f = 1; char c = G();
        while(c < '0' || c > '9') {if(c == '-') f = -1; c = G(); }
        while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = G(); }
        x *= f;
    }
    
    template<class I>
    inline void write(I x) {
        if(x == 0) {putchar('0'); return;}
        I tmp = x > 0 ? x : -x;
        if(x < 0) putchar('-');
        int cnt = 0;
        while(tmp > 0) {
            buf1[cnt++] = tmp % 10 + '0';
            tmp /= 10;
        }
        while(cnt > 0) putchar(buf1[--cnt]);
    }
    
    #define in(x) read(x)
    #define outn(x) write(x), putchar('
    ')
    #define out(x) write(x), putchar(' ')
    
    } using namespace io;
    
    #define ll long long
    const int N = 100010;
    
    int main() {
    
    }
  • 相关阅读:
    随笔--摘录
    在windows+Linux环境下搭建Hadoop集群
    大数据 hadoop 环境搭建
    react-native 学习 ----- React Navigation
    android开发中关于继承activity类中方法的调用
    linux下 bin和sbin的区别
    inux中bin与sbin目录的作用及区别介绍
    hadoop 解除 "Name node is in safe mode"
    hadoop常用命令
    菜鸟使用MySQL存储过程and临时表,供新手参考,请高手斧正
  • 原文地址:https://www.cnblogs.com/henry-1202/p/9914043.html
Copyright © 2011-2022 走看看