跳到正文

个人记录

语法 / STL 热知识

Bolaris0717约 14 分钟
原文
文章信息与版本

文章信息

发布
2023年5月21日
收录
2026年3月30日
更新
2026年11月11日
篇幅
约 14 分钟

版本

  1. v2当前
  2. v1

Mod.

白科技!!

建议配合 Ctrl + F 食用。

踩坑

123

lower_bound/upper_bound

  • lower_bound(l,r,w,cmp):找到 [l,r) 首个使 cmp(*it,w) 为 false 的 it。需要保证所有 true 在 false 之前。
  • upper_bound(l,r,w,cmp):找到 [l,r) 首个使 cmp(w,*it) 为 true 的 it。需要保证所有 false 在 true 之前。

cmp 的默认传参为 less。

vector

push_back 2e6.

set

cache 命中得可怜。

setprecision

注意 0.5 是不进一的,要严格大于 0.5 才会。

deque

deque 是加强的 vector?

初始化时一般创建八倍元素类型大小空间

swap()

注意 swap 两个数组复杂度是数组大小,但是 swap 两个 vector 是 O(1) 的。

find(l,r,x)

找到区间 [l,r)[l,r) 内第一个等于 xx 的数。

复杂度线性。

__lg(x)

返回 ⌊log⁡2n⌋\lfloor \log_2 n \rfloor 。

复杂度 O(1)\mathcal{O(1)}。

minmax(a,b)

返回 pair,first 是 min,second 是 max。

min_element / max_element / minmax_element

返回对应极值的位置。

for_each

顾名思义。

nth_element(l,mid,r)

区间第 kk 小:k-=1,nth_element(begin(a),begin(a)+k,end(a)),cout<<a[k]<<'\n';

将 [l,r)[l,r) 重排序,使得 [l,mid)[l,mid) 中不超过 midmid,(mid,r)(mid,r) 不少于 midmid。

复杂度 O(n)\mathcal{O(n)}。

stable_sort

稳定的 sort。

is_sorted

是否不降。

rotate

循环轮转。

rotate(l,mid,r) 将 [l,r)[l,r) 转到 midmid 指向 ll。

复杂度 O(n)\mathcal{O(n)}。

fill

区间赋值。

merge / inplace_merge

end=merge(l1,r1,l2,r2,begin)

inplace_merge(l,mid,r)

归并排序。

count / count_if

计数器。

transform

a+b=ca+b=c。

partial_sum / adjacent_difference

好像都是左闭右闭的,不牛。前缀和 si=si−1+ais_i = s_{i - 1} + a_i / 差分 di=ai−ai−1d_i = a_i - a_{i - 1}

个人:si+1=si+ais_{i + 1} = s_i + a_i,di+ai=ai+1d_i + a_i = a_{i + 1},这样就都是左开右闭,很统一,美中不足的是,要多开 sns_n,dn−1d_{n-1} 没有用,可以用来记录 a0a_0。

hypot

hypot(x,y)=x2+y2=\sqrt{x^2+y^2}。

functional

代码
plus<>;//x+y
minus<>;//x-y
multiplies<>;//x*y
divides<>;//x/y
modulus<>;//x%y
negate<>;//-x

equal_to<>;//x==y
not_equal_to<>;//x!=y
greater<>;//x>y
less<>;//x<y
greater_equal<>;//x>=y
less_equal<>;//x<=y

logical_and<>;//x&&y
logical_or<>;//x||y
logical_not<>;//!x

bit_and<>;//x&y
bit_or<>;//x|y
bit_xor<>;//x^y
bit_not;//~x

__builtin_xxx / __builtin_xxxll

  • __builtin_popcount:返回 11 的个数。

  • __builtin_parity:返回 11 的个数奇偶性。

  • __builtin_ffs:最后一个 1 是从低往高第几位,是 lg(lowbit)+1。

  • __builtin_sqrt:开平方,没有取整。

  • __builtin_ctz:后导 00 个数,要求 x≠0x \neq 0。

  • __builtin_clz:前导 00 个数,要求 x≠0x \neq 0。

  • __builtin_clrsb:最长同色前缀长度 −1-1。

iota

生成连续的数。

partition

将一个范围内的元素划分为两个部分,给定一元谓词为 True 的元素在前面,其他在后面。

is_partitioned

检查一个范围内的元素是否已经被划分为两个连续部分,第一部分满足特定的一元谓词,第二部分不满足一元谓词。

partition_point

在 [l,r)[l,r) 中二分出第一个不满足该谓词的元素,要保证单调性。

string.append

string.append(str) 等价于 +=str

string.append(str,ind,len) 添加 str 从 ind 开始的 len 个元素

string.append(x,c) 添加 xx 个 cc

string.append(l,r) 添加 [l,r)[l,r) 的元素(强制转成 char)

string.replace

string.replace(ind,len,str,Ind=0,Len=str.size()),将 [ind,ind+len)[ind,ind+len) 替换为 str[Ind,Ind+Len)str[Ind,Ind+Len)。

str,Ind,Len 亦可以替换为 c,x。

string.find/rfind

str,ind.

find 从前往后匹配,返回第一个匹配串左端点,找不到返回 −1-1。 rfind 从后往前匹配,返回第一个匹配串右端点,找不到返回 −1-1。

string.find_first_not_of / find_last_not_of

找不匹配的。

string.substr

ind,len

但是 string 几个成员函数复杂度都是和原字符串长有关的。-.-!

all_of / any_of

l,r,F

& 和, | 和。

find_if | find_not

找到一个满足 / 不满足谓词的指针。

注意这个是线性的,不是二分。

equal_range

[l,r)[l,r) 中等于 xx 的范围。

要求升序。复杂度 log⁡\log

adjacent_find

[l,r)[l,r) 中最左的相邻相同位置,即查找 2 个连续相等

remove / remove_if / replace / replace_if

删除 / 替换。

注意替换是先传 old 再传 new

swap_ranges

swap,(l1,r1,l2)

[l,r)[l,r) 是否存在 xx。

要求升序,复杂度 log⁡\log。

boolalpha

islower, isupper, isdigit, isalpha, isalnum, toupper, tolower

stringstream

结合 ofstream 批量造数据。

C++
ofstream cout;
void File(int T,string s) {
  stringstream is;
  is<<s<<"_"<<setw(3)<<setfill('0')<<T<<".in";
  cout.close();
  cerr<<is.str()<<'\n';
  cout.open(is.str());
}
void Solve() {
  return;
}
void main() {
  ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
  int l=7,r=17;
  for(int i=l;i<r;++i) {
    File(i,"BK0717"),Solve();
  }
}

STL pbds

#include<bits/extc++.h> using namespace __gnu_pbds;

红黑树

tree<Info1,Info2,less<>,rb_tree_tag,tree_order_statistics_node_update>t; // Info1 -> Info2(null_type)

成员函数: find, insert(返回 pair<it,bool>), erase, lower_bound, upper_bound 同 set; order_of_key(w): 找到键值 w 的排名(是小于 ww 的值的个数) find_by_order(k): 第 k 大的指针 join(x): 把 x 合并进来,并删除 x split(val,t): 保留 ≤\le val 的部分,>> val 的放在 b 里

pair_heap

__gnu_pbds::priority_queue<int>q;

attribute

C++
__attribute__((constructor)) void before_main() {
   clg("--- ",__func__,'\n');
}
__attribute__((destructor)) void after_main() {
   clg("--- ",__func__,'\n');
}
struct __attribute__((packed)) Info {
  int x; LL y;
};

ws / noskipws / skipws

从输入流舍弃前导空白符。

noskipws:保留前导字符的输入。

setbase

setbase(8/10/16) 按对应的进制输入。

showpos / noshowpos

启用 / 关闭 输出非负整数的 +。

exchange

exchange(a,b):以 b 替换 a 的值,并返回 a 的旧值。

chrono

更好的测时工具。

chrono::high_resolution_clock::now():返回一个 timepoint。

std::chrono::duration<double>.count():输出以 s 为单位。

C++
struct {
  chrono::time_point<chrono::high_resolution_clock>S,T;
  void operator~() {
    S=chrono::high_resolution_clock::now();
  }
  void operator-() {
    T=chrono::high_resolution_clock::now();
  }
  void operator!() {
    cerr<<std::chrono::duration<double>{T-S}.count()<<"s\n";
  }
}Time;
~Time,-Time,!Time;

quoted

quoted(s,c='"'):输出 c+s+c

gprof

gcc 编译的时候加个参数 -pg,然后运行程序,正常的话会产生一个gprof.out,然后 gprof ./xx。

待验证。

ac library

拉伸二象性

中文:luogu.com.cn/article/xxa80t4t

英文:https://atcoder.github.io/ac-library/production/document_en/appendix.html

默认都是 0-index,左闭右开

功能、要求、复杂度

convolution †\dag

改 appendix.html 为 convolution.html 即可看到英文版 https://atcoder.github.io/ac-library/production/document_en/convolution.html

dsu

  • dsu d(int n)

  • int d.merge(int a, int b)

  • bool d.same(int a, int b)

  • int d.leader(int a)

  • int d.size(int a)

  • vector<vector<int>> d.groups()

fenwicktree

  • fenwick_tree<T> fw(int n)

    • T 只能是整数
  • void fw.add(int p, T x)

    • a[p] += x
  • T fw.sum(int l, int r)

    • [l, r)

lazysegtree †\dag

https://atcoder.github.io/ac-library/production/document_en/lazysegtree.html

math

  • ll pow_mod(ll x, ll n, int m)
    • 快速幂 xn mod mx^n \bmod m
    • 0≤n,1≤m0 \le n, 1 \le m
  • ll inv_mod(ll x, ll m)
    • 1x mod m\frac 1x \bmod m
    • (x,m)=1,1≤m(x, m) = 1, 1 \le m
    • O(log⁡m)O(\log m)
  • pair<ll, ll> crt(vector<ll> r, vector<ll> m) †\dag
  • ll floor_sum(ll n, ll m, ll a, ll b) †\dag
  • https://atcoder.github.io/ac-library/production/document_en/math.html

maxflow †\dag

https://atcoder.github.io/ac-library/production/document_en/maxflow.html

mincostflow †\dag

https://atcoder.github.io/ac-library/production/document_en/mincostflow.html

modint

就是 Mi. https://atcoder.github.io/ac-library/production/document_en/modint.html

scc †\dag

https://atcoder.github.io/ac-library/production/document_en/scc.html

segtree †\dag

https://atcoder.github.io/ac-library/production/document_en/segtree.html

string †\dag

有 SA 和 Z 算法 https://atcoder.github.io/ac-library/production/document_en/string.html

twosat †\dag

https://atcoder.github.io/ac-library/production/document_en/twosat.html

QwQ。

原文