日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++快速输入输出优化

發(fā)布時間:2025/3/16 c/c++ 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。