Appearance
GCC、Clang 等编译器提供的非 cpp 标准函数
| 函数 | 功能 |
|---|---|
| __builtin_popcount | 统计 unsigned int x 的二进制中 |
| __builtin_ctz | 统计 unsigned int x 的二进制中末尾 |
| __builtin_clz | 统计 unsigned int x 的二进制中开头 |
| __builtin_parity | 统计 unsigned int x 的二进制中 |
__builtin_ctz/__builtin_clz 中传入
以上函数时间复杂度均为
以上函数均有在末尾加 l 表示参数 unsigned long 和在末尾加 ll 表示参数 unsigned long long 的变形。
需要正确传入参数,否则会触发未定义行为。
C++20 引入了 <bit> 头文件
| 函数 | 功能 |
|---|---|
| popcount | 统计 |
| countr_zero | 统计 |
| countl_zero | 统计 |
| countr_one | 统计 |
| countl_one | 统计 |
| bit_width | 对于 |
| bit_floor | 对于 |
| bit_ceil | 对于 |
countr_zero 可以传入
以上函数参数均为无符号整数,自动识别是 unsigned int 还是 unsigned long long。
以上函数时间复杂度均为
mt19937
cpp
mt19937 rng(time(nullptr));
int rnd(int l, int r) {
uniform_int_distribution<int> dist(l, r);
int num = dist(rng);
return num;
}
vector<int> a;
shuffle(a.begin(), a.end(), rng);nth-element
cpp
// 默认升序规则(less<>)
nth_element(first, nth, last);
// 自定义比较规则
nth_element(first, nth, last, cmp);
// 自定义降序比较规则
nth_element(nums.begin(), nums.begin() + k - 1, nums.end(),
[](int a, int b) { return a > b; });