版本比较
C++常用代码模版1
标题
增加 1 行删除 1 行
11C++常用代码模版C++常用代码模版1正文
增加 116 行删除 84 行
123456781234567891011121314151617181920212223112425262728293031323314 行未改动484950513552535455568 行未改动656667685253697071727374757677787963648081828384854 行未改动90919293777894959697989957 行未改动1571581591601441611621461471631641651661671491501511521531681691701711541551561571581591601611621631641651661721681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142151741751761771781791801811821831841851861871881891901911922162171941952182191971981992002012022032042052062202212222232242252262272282292082092302312322332342352362152372382392402192412422432442232452462472482492502512302522532542332552562572582592602392402612622632642652663 行未改动2702712722732522742752762772784 行未改动283284285286265287288289290291905 行未改动全部展开11971198119912001201120212031204120512061207120812091210121112121213:::info[C++系列]链接**直达洛谷**,跳转后请将`.com.cn`替换为`.me`- $26/05/11$ [C++常用代码模版](https://www.luogu.com.cn/article/s4barriq) **当前文章**- $25/12/25$ [C++代码优化](https://www.luogu.com.cn/article/icirmjgn)- $25/08/06$ [C++里的字符串处理函数](https://www.luogu.com.cn/article/tksqjg4g)- $24/10/03$ [C++常用知识](https://www.luogu.com.cn/article/55bt4q2k):::### **前言:可转载,但需标明出处。更新日志及其他内容将在评论区写出**
### [链接中转站🔗](https://www.luogu.com.cn/article/9nc535qu)—[保存站](https://www.luogu.me/article/9nc535qu)

# Tips & Attention & Warning- 查看文章时建议在保存站里使用“聚焦”模式,查看评论可使用“综合”模式。- 部分**大型**模版代码没有定义在命名空间下,为了 保证代码的**可读性**与**全局函数命名冲突**等情况。请在使用时**酌情考虑**是否放入命名空间中。 这里给出命名空间的定义代码: ```cpp namespace 名称{ 你的代码 …… } ``` 使用时可以`using namespace 名称;`来使用;也可以**在不写上一条语句时**,`命名空间名称::函数名()`这样使用。如果在多个命名空间有**名称与形参都相同的函数**时,不能`函数名(实参,...);`这样写,必须第二种方法,在函数名前加上`函数所在的具体命名空间::`这样写才正确。- 常用数据范围: 
# 快读快写## 提示
**拓展链接:**- [更多方法](https://www.luogu.com.cn/article/ezv2h8q1)- [OI Wiki——读入、输出优化](https://oi-wiki.org/contest/io/)
## 提示`普通cin < 关同步cin < scanf < void引用快读 < int返回快读 < 缓冲区整块快读`
## 更快的快读:关闭同步流版(cin/cout 加速)```cpp14 行未改动## int + long long 快读 整合模板```cpp#include<bits/stdc++.h>using namespace std;
using ll=long long;namespace mystd { // int 快读 inline int read(){ int x=0,f=1;8 行未改动 } return x*f; }
//int快写 inline void write(long long x) { //int快写 inline void write(ll x) { if(x<0){ putchar('-'); x=-x; } if(x>9)write(x/10); putchar(x%10+'0'); }
//longlong快读 inline long long readll(){ long long x=0,f=1; inline ll readll(){ ll x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-')f=-1; ch=getchar();4 行未改动 } return x*f; }
//longlong快写 inline void write(long long x) { //longlong快写 inline void write(ll x) { if(x<0){ putchar('-'); x=-x; }57 行未改动}```:::
## 【天花板最快】缓冲区整块快读快写(巨量数据专用)## 兼容`int`和`long long`类型的缓冲区整块快读快写```cppconst int BUF = 1 << 20; // 1MB缓冲区,和快读保持一致char buf[BUF], *p1 = buf, *p2 = buf;namespace keyvalue { const int BUF=1<<20; char ibuf[BUF],*p1=ibuf,*p2=ibuf; char obuf[BUF],*op=obuf;
// 快读(你原来的代码,保留)inline char gc() { if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, BUF, stdin); return *p1++;} inline char gc() { if(p1==p2) p2=(p1=ibuf)+fread(ibuf,1,BUF,stdin); return *p1++; }inline int read() { int x = 0, f = 1; char ch = gc(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = gc(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = gc(); } return x * f;}
// ===================== 快写核心 =====================// 刷新缓冲区(必须写)inline void flush() { fwrite(buf, 1, p1 - buf, stdout); p1 = buf; inline void flush() { fwrite(obuf,1,op-obuf,stdout); op=obuf; }
inline void pc(char ch) { if(op==obuf+BUF) flush(); *op++=ch; }
inline int read() { int x=0,f=1;char ch=gc(); while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=gc();} while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=gc();} return x*f; }
inline long long readll() { long long x=0;int f=1;char ch=gc(); while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=gc();} while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=gc();} return x*f; }
inline void write(int x) { if(x<0) pc('-'),x=-x; char sta[20];int top=0; do sta[top++]=x%10+'0'; while(x/=10); while(top) pc(sta[--top]); }
inline void write(long long x) { if(x<0) pc('-'),x=-x; char sta[20];int top=0; do sta[top++]=x%10+'0'; while(x/=10); while(top) pc(sta[--top]); }
inline void writesp(int x) {write(x),pc(' ');} inline void writesp(long long x) {write(x),pc(' ');} inline void writeln(int x) {write(x),pc('\n');} inline void writeln(long long x) {write(x),pc('\n');}}// 单字符输出inline void pc(char ch) { if (p1 == p2 + BUF) flush(); // 满了就刷 *p1++ = ch;}// 整数输出(支持正负数)inline void write(int x) { if (x < 0) pc('-'), x = -x; // 递归/数组逆序输出都可以,这里用数组更稳 char sta[20]; int top = 0; do sta[top++] = x % 10 + '0'; while (x /= 10); while (top) pc(sta[--top]);}// 输出后加空格(常用)inline void writesp(int x) { write(x), pc(' '); }// 输出后加换行(常用)inline void writeln(int x) { write(x), pc('\n'); }using namespace keyvalue;```
### 配套使用示例:### 使用实例```cppint main() { int a = read(); int b = read(); writesp(a); // 输出a+空格 writeln(b); // 输出b+换行 //【最后必须刷新!】 flush(); return 0;}int a = read();long long b = readll();writesp(a);writeln(b);flush(); // 程序结束务必手动flush,防止输出残留在缓冲区丢失```### 说明:>1. C++不能对返回值重载,所以long long读入命名为`readll()`,`read()`专用于int;>2. `write/writesp/writeln`依靠参数类型自动重载,传`int`走int分支,传`long long`走ll分支
### 核心注意事项#### 程序结束前 **必须调用 flush()**## 核心注意事项### 程序结束前 **必须调用 flush()**这是最容易踩的坑!- 快写是把数据先存在内存缓冲区,**满了才自动输出**;- 程序结束时缓冲区可能没满,数据会**留在内存里不输出**,直接导致答案错误。- 必须在 `main` 最后一行写 `flush();`。
#### 不要混用 cin/cout/printf/puts### 不要混用 cin/cout/printf/puts- 混用会**打乱缓冲区**,导致输出顺序错乱、丢失数据;- 整套输入输出**只用 read() + write() 系列函数**。
#### 缓冲区大小建议### 缓冲区大小建议- 你用的 `1<<20`(1MB)是**竞赛标准最优值**;- 不要开太大(超过 8MB 可能爆栈),不要开太小(频繁刷新变慢)。
#### 数据范围适配### 数据范围适配- 上面的 `write()` 写的是 `int`;- 如果要输出 **long long**,把函数参数改成 `long long` 即可: ```cpp inline void write(ll x) { ... } ```
#### 多组输出/循环不用手动 flush### 多组输出/循环不用手动 flush只有**程序结束**时 flush 一次就行,中间满了会自动刷新,手动刷会变慢。
### 优势## 优势1. **速度天花板**:比 `scanf/printf` 快 5到10 倍,比普通 `getchar` 快读快 2到3 倍;2. **巨量数据专用**:1e8/1e9 读入写出无压力,完美解决卡时间题;3. **通用性拉满**:洛谷、Codeforces、NOIP、省选、国赛全支持;4. **代码轻量**:几十行,不占空间,不依赖任何库。
## 快速幂### 不取模# 快速幂## 不取模```cppint quickpower(long long a,long long b){ long long ans=1,base=a; while(b){3 行未改动 } return ans;}```### 固定取模## 固定取模```cppconst int mod=1e9+7;int quickpower(long long a,long long b){ long long ans=1,base=a%mod;4 行未改动 } return ans;}```### 形参取模## 形参取模```cppint quickpower(long long a,long long b,long long p){ long long ans=1,base=a; while(b){905 行未改动全部展开```cppint cnt[26]={0};for(char ch:s) cnt[ch-'a']++;```### 输出```cppstring s;cin>>s;int cnt[26]= {0};for(char ch:s) cnt[ch-'a']++;int c=0;for(int x:cnt)cout<<(char)('a'+c)<<' '<<x<<'\n',c++;```
:::error[你知道吗(恶搞]- MOD=998244353=`(114514*(54-1+114*(1+14*5+1+4)))+(4+11451*(4-1-15+14))+(11+41*54+(141+541))+(4-1-15+14)`:::