版本比较
工具头和函数
正文
增加 13 行删除 13 行
11 行未改动121314151616171819202021222324242526272811 行未改动4041424344444546474748495050515253549 行未改动64656667686869707172727374757669 行未改动146147148149150150151152153154224 行未改动全部展开379380381382383383384385386387214 行未改动全部展开60260360460560660660760860961088 行未改动699700701702703703704705706707240 行未改动全部展开94894995095195295295395495595673 行未改动11 行未改动int main() { // 竞赛中常见的静态分数(比如题目给出的固定比例) using r1 = ratio<12, 18>; // 题目给定的分数1:12/18 using r2 = ratio<5, 9>; // 题目给定的分数2:5/9
// 编译期求和+自动约分(无需手写gcd约分) using sum = ratio_add<r1, r2>; cout << "12/18 + 5/9 = " << sum::num << "/" << sum::den << endl; // 输出11/9
// 编译期比较:判断和是否等于11/9(避浮点比较陷阱) using target = ratio<11, 9>; cout << "和是否等于11/9:" << boolalpha << ratio_equal<sum, target>::value << endl;
return 0;}// 豆包生成```::::11 行未改动
int main() { // OI中常见的数组/vector场景 vector<int> a = {1, 2, 2, 3, 3, 3, 4};
// 查找第一个相邻相等的元素对 auto it = adjacent_find(a.begin(), a.end());
if (it != a.end()) { // 输出:找到的元素值为2,下标为1(OI中常需要下标) cout << "找到第一个相邻相等的元素:" << *it cout << "找到第一个相邻相等的元素:" << *it << ",下标:" << it - a.begin() << endl; } else { cout << "无相邻相等的元素" << endl; }9 行未改动using namespace std;
int main() { vector<int> a = {1, 2, 3, 2, 4, 1};
// 自定义条件:相邻两个元素的和等于5 auto it = adjacent_find(a.begin(), a.end(), [](int x, int y) { return x + y == 5; });
if (it != a.end()) { cout << "找到第一个和为5的相邻对:" << *it << " 和 " << *(it+1) << ",下标:" << it - a.begin() << endl; } else {69 行未改动using namespace std;
int main() { vector<int> a = {1, 2, 3, 4, 5, 6, 7, 8};
// 场景1:统计偶数的和 int even_sum = accumulate(a.begin(), a.end(), 0, [](int res, int x) { return res + (x % 2 == 0 ? x : 0); // 偶数加x,奇数加0224 行未改动全部展开}```::::
## 【构造】transform ## 【构造】transform
基于 `#include<algorithm>`,对一个或两个输入容器的元素执行自定义**转换/运算**,将结果写入输出容器(或原地修改原容器),$O\left(n\right)$。
::::info[批量处理单个容器元素示例代码]214 行未改动全部展开`size` 获取范围的大小作为无符号值。
`ssize` 获取范围的大小作为带符号值。
## 【查询】regex_match ## 【查询】regex_match
检查整个字符串是否完全符合正则表达式的规则(而非部分匹配),返回bool值表示“完全匹配”或“不匹配”。
::::warning88 行未改动
int main() { string s = "OI2026竞赛123题456"; // 正则规则:\\d → 匹配单个数字;替换格式串:* → 把数字换成* regex reg("\\d"); regex reg("\\d"); string res = regex_replace(s, reg, "*");
cout << "原字符串:" << s << endl; // OI2026竞赛123题456 cout << "替换后:" << res << endl; // OI****竞赛***题***240 行未改动全部展开}```::::
## 【查询】__builtin_bitreverse ## 【查询】__builtin_bitreverse
反转 $32$ 位二进制位(如 $0001$→$1000$)。其实就是变成字符串后 reverse。
::::info[示例代码]73 行未改动