C++快速输入输出优化
生活随笔
收集整理的這篇文章主要介紹了
C++快速输入输出优化
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在這里存一下我的快速輸入輸出優(yōu)化
以及寫題模板
這里的是$getchar$優(yōu)化和$putchar$優(yōu)化,$fread$和$fwrite$暫時咕咕咕
fread已補(bǔ)
快速輸入
這里$define$了一個$I\_int$,改讀入的數(shù)據(jù)類型的話直接在$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優(yōu)化板子
封裝到了一個$namespace$里面,寫題的時候可以收起來看著會比較舒服QAQ
namespace io {#define in(a) a=read()#define out(a) write(a)#define outn(a) out(a),putchar('\n')#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讀入優(yōu)化
就是把原來那個$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; 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 #endiftemplate<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) {buf[cnt++] = tmp % 10 + '0';tmp /= 10;}while(cnt > 0) putchar(buf[--cnt]); }#define in(x) read(x) #define outn(x) write(x), putchar('\n') #define out(x) write(x), putchar(' ')} using namespace io;#define ll long long const int N = 100010;int main() {}?
轉(zhuǎn)載于:https://www.cnblogs.com/henry-1202/p/9914043.html
總結(jié)
以上是生活随笔為你收集整理的C++快速输入输出优化的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 南通大学16级软嵌班软件工程课程成绩汇总
- 下一篇: python学习笔记04 -------