STL 算法大全

C++标准模板库算法完整参考指南,包含详细说明、代码示例和时间复杂度分析

std::sort

快速排序算法

对范围 [first, last) 中的元素进行排序,默认按升序排序。

#include <algorithm>
#include <vector>

std::vector<int> v = {5, 3, 1, 4, 2};
std::sort(v.begin(), v.end());
// v 现在为 {1, 2, 3, 4, 5}
平均时间复杂度: O(n log n)

std::find

查找元素

在范围 [first, last) 中查找等于 value 的元素。

std::vector<int> v = {10, 20, 30, 40};
auto it = std::find(v.begin(), v.end(), 30);
if (it != v.end()) {
  std::cout << "找到元素: " << *it;
}
时间复杂度: O(n)

std::transform

应用函数到范围

将一元操作应用于输入范围,并将结果存储在目标范围中。

std::vector<int> v1 = {1, 2, 3, 4};
std::vector<int> v2(4);
std::transform(v1.begin(), v1.end(), v2.begin(),
  [](int n) { return n * n; });
// v2 现在为 {1, 4, 9, 16}
时间复杂度: O(n)

std::accumulate

范围求和

计算范围内元素的累加和,或使用二元操作进行累积计算。

std::vector<int> v = {1, 2, 3, 4};
int sum = std::accumulate(v.begin(), v.end(), 0);
// sum = 10

int product = std::accumulate(v.begin(), v.end(), 1,
  [](int a, int b) { return a * b; });
// product = 24
时间复杂度: O(n)

std::copy

复制范围

将范围 [first, last) 中的元素复制到从 d_first 开始的另一范围。

std::vector<int> src = {1, 2, 3, 4};
std::vector<int> dest(4);
std::copy(src.begin(), src.end(), dest.begin());
// dest 现在为 {1, 2, 3, 4}
时间复杂度: O(n)

std::remove

移除元素

从范围 [first, last) 中移除所有等于 value 的元素,但不改变容器大小。

std::vector<int> v = {1, 2, 3, 2, 5};
auto new_end = std::remove(v.begin(), v.end(), 2);
v.erase(new_end, v.end());
// v 现在为 {1, 3, 5}
时间复杂度: O(n)

std::reverse

反转范围

反转范围 [first, last) 中元素的顺序。

std::vector<int> v = {1, 2, 3, 4};
std::reverse(v.begin(), v.end());
// v 现在为 {4, 3, 2, 1}
时间复杂度: O(n)

std::unique

删除连续重复元素

删除范围 [first, last) 中连续的重复元素。

std::vector<int> v = {1, 1, 2, 2, 3, 3, 3, 4};
auto last = std::unique(v.begin(), v.end());
v.erase(last, v.end());
// v 现在为 {1, 2, 3, 4}
时间复杂度: O(n)

std::count

计数元素

返回范围 [first, last) 中等于 value 的元素数。

std::vector<int> v = {1, 2, 3, 2, 4, 2};
int count = std::count(v.begin(), v.end(), 2);
// count = 3
时间复杂度: O(n)

std::max_element

查找最大元素

返回范围 [first, last) 中最大元素的迭代器。

std::vector<int> v = {3, 1, 4, 2, 5};
auto max_it = std::max_element(v.begin(), v.end());
std::cout << "最大值: " << *max_it;
// 输出: 最大值: 5
时间复杂度: O(n)

std::binary_search

二分查找

检查范围 [first, last) 中是否存在等价于 value 的元素。

std::vector<int> v = {1, 2, 3, 4, 5, 6, 7};
bool found = std::binary_search(v.begin(), v.end(), 4);
// found = true
bool not_found = std::binary_search(v.begin(), v.end(), 8);
// not_found = false
时间复杂度: O(log n)

std::fill

填充范围

将给定值复制赋值给范围 [first, last) 中的每个元素。

std::vector<int> v(5); // {0, 0, 0, 0, 0}
std::fill(v.begin(), v.end(), 10);
// v 现在为 {10, 10, 10, 10, 10}
时间复杂度: O(n)

std::replace

替换元素

将范围 [first, last) 中所有等于 old_value 的元素替换为 new_value。

std::vector<int> v = {1, 2, 3, 2, 4};
std::replace(v.begin(), v.end(), 2, 9);
// v 现在为 {1, 9, 3, 9, 4}
时间复杂度: O(n)

std::rotate

旋转范围

将范围 [first, last) 中的元素旋转,使得 middle 成为新的第一个元素。

std::vector<int> v = {1, 2, 3, 4, 5};
std::rotate(v.begin(), v.begin() + 2, v.end());
// v 现在为 {3, 4, 5, 1, 2}
时间复杂度: O(n)

std::partition

划分范围

将满足谓词的元素置于不满足谓词的元素之前。

std::vector<int> v = {1, 2, 3, 4, 5, 6};
auto it = std::partition(v.begin(), v.end(),
  [](int i) { return i % 2 == 0; });
// v 可能为 {2, 4, 6, 1, 3, 5}
时间复杂度: O(n)

std::nth_element

部分排序

对范围部分排序,确保第n个元素是排序后该位置应有的元素。

std::vector<int> v = {5, 3, 1, 6, 4, 2};
std::nth_element(v.begin(), v.begin() + 2, v.end());
// v[2] 现在是3,且左侧元素≤3,右侧元素≥3
平均时间复杂度: O(n)

std::all_of

检查所有元素

检查范围 [first, last) 中所有元素是否都满足谓词。

std::vector<int> v = {2, 4, 6, 8};
bool allEven = std::all_of(v.begin(), v.end(),
  [](int i) { return i % 2 == 0; });
// allEven = true
时间复杂度: O(n)

std::inner_product

内积计算

计算两个范围的内积(点积)或执行自定义操作。

std::vector<int> a = {1, 2, 3};
std::vector<int> b = {4, 5, 6};
int result = std::inner_product(a.begin(), a.end(),
  b.begin(), 0);
// result = 1*4 + 2*5 + 3*6 = 32
时间复杂度: O(n)

std::set_intersection

交集运算

构造两个排序范围的交集。

std::vector<int> v1 = {1, 2, 3, 4};
std::vector<int> v2 = {3, 4, 5, 6};
std::vector<int> v_intersection;
std::set_intersection(v1.begin(), v1.end(),
  v2.begin(), v2.end(),
  std::back_inserter(v_intersection));
// v_intersection = {3, 4}
时间复杂度: O(2*(n+m))

std::minmax_element

查找最小和最大元素

返回范围中最小和最大元素的迭代器。

std::vector<int> v = {3, 1, 4, 2, 5};
auto [min, max] = std::minmax_element(v.begin(), v.end());
std::cout << "最小值: " << *min << ", 最大值: " << *max;
// 输出: 最小值: 1, 最大值: 5
时间复杂度: O(n)

std::adjacent_find

查找相邻重复元素

在范围中搜索第一对相邻的重复元素。

std::vector<int> v = {1, 2, 2, 3, 4, 4, 5};
auto it = std::adjacent_find(v.begin(), v.end());
if (it != v.end()) {
  std::cout << "相邻重复: " << *it << " 和 " << *(it+1);
}
时间复杂度: O(n)

std::generate

生成元素

将函数生成的值赋给范围中的每个元素。

std::vector<int> v(5);
int n = 1;
std::generate(v.begin(), v.end(), [&n]() {
  return n++;
});
// v 现在为 {1, 2, 3, 4, 5}
时间复杂度: O(n)

std::is_sorted

检查是否排序

检查范围是否按升序排序。

std::vector<int> v1 = {1, 2, 3, 4};
std::vector<int> v2 = {4, 2, 1, 3};
bool sorted1 = std::is_sorted(v1.begin(), v1.end()); // true
bool sorted2 = std::is_sorted(v2.begin(), v2.end()); // false
时间复杂度: O(n)

std::partial_sum

部分和计算

计算范围的前缀和。

std::vector<int> v = {1, 2, 3, 4};
std::vector<int> result(4);
std::partial_sum(v.begin(), v.end(), result.begin());
// result 现在为 {1, 3, 6, 10}
时间复杂度: O(n)

std::copy_if

条件复制

复制范围中满足特定条件的元素到目标范围。

std::vector<int> src = {1, 2, 3, 4, 5};
std::vector<int> dest;
std::copy_if(src.begin(), src.end(), std::back_inserter(dest),
  [](int n) { return n % 2 == 0; });
// dest 现在为 {2, 4}
时间复杂度: O(n)

std::merge

合并排序范围

合并两个已排序范围到一个新的排序范围中。

std::vector<int> v1 = {1, 3, 5};
std::vector<int> v2 = {2, 4, 6};
std::vector<int> merged(6);
std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), merged.begin());
// merged 现在为 {1, 2, 3, 4, 5, 6}
时间复杂度: O(n + m)

std::find_if

条件查找

在范围中查找满足特定条件的第一个元素。

std::vector<int> v = {1, 3, 5, 7, 9};
auto it = std::find_if(v.begin(), v.end(),
  [](int n) { return n > 5; });
if (it != v.end()) {
  std::cout << "找到大于5的元素: " << *it;
}
时间复杂度: O(n)

std::for_each

对每个元素应用函数

对范围中的每个元素应用指定的函数。

std::vector<int> v = {1, 2, 3, 4};
std::for_each(v.begin(), v.end(),
  [](int& n) { n *= n; });
// v 现在为 {1, 4, 9, 16}
时间复杂度: O(n)

std::iota

填充递增序列

用连续递增的值填充范围。

std::vector<int> v(5);
std::iota(v.begin(), v.end(), 10);
// v 现在为 {10, 11, 12, 13, 14}
时间复杂度: O(n)

std::mismatch

查找不匹配点

返回两个范围中第一个不匹配的元素对。

std::string s1 = "hello world";
std::string s2 = "hello there";
auto [it1, it2] = std::mismatch(s1.begin(), s1.end(), s2.begin());
// *it1 = 'w', *it2 = 't'
时间复杂度: O(min(n, m))

std::set_difference

差集运算

构造两个排序范围的差集(在第一个范围但不在第二个范围中)。

std::vector<int> v1 = {1, 2, 3, 4, 5};
std::vector<int> v2 = {3, 4, 5, 6, 7};
std::vector<int> diff;
std::set_difference(v1.begin(), v1.end(),
  v2.begin(), v2.end(), std::back_inserter(diff));
// diff 现在为 {1, 2}
时间复杂度: O(2*(n+m))

std::shuffle

随机重排

使用随机数生成器对范围中的元素进行随机重排。

std::vector<int> v = {1, 2, 3, 4, 5};
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(v.begin(), v.end(), g);
// v 可能为 {3, 1, 5, 2, 4}
时间复杂度: O(n)

std::stable_sort

稳定排序

对范围进行排序,同时保持相等元素的相对顺序。

std::vector<std::pair<int, char>> v = {
  {2, 'b'}, {1, 'a'}, {2, 'c'}, {1, 'd'}};
std::stable_sort(v.begin(), v.end(),
  [](auto a, auto b) { return a.first < b.first; });
// 排序后: {1, 'a'}, {1, 'd'}, {2, 'b'}, {2, 'c'}
时间复杂度: O(n log² n)

std::includes

检查子集

检查一个排序范围是否包含另一个排序范围的所有元素。

std::vector<int> v1 = {1, 2, 3, 4, 5};
std::vector<int> v2 = {2, 4};
bool result = std::includes(v1.begin(), v1.end(),
  v2.begin(), v2.end());
// result = true
时间复杂度: O(n + m)

std::remove_if

条件移除

从范围中移除满足特定条件的元素。

std::vector<int> v = {1, 2, 3, 4, 5, 6};
auto new_end = std::remove_if(v.begin(), v.end(),
  [](int n) { return n % 2 == 0; });
v.erase(new_end, v.end());
// v 现在为 {1, 3, 5}
时间复杂度: O(n)

std::unique_copy

复制唯一元素

从范围中复制元素,忽略连续的重复项。

std::vector<int> src = {1, 1, 2, 2, 3, 3, 3, 4};
std::vector<int> dest;
std::unique_copy(src.begin(), src.end(),
  std::back_inserter(dest));
// dest 现在为 {1, 2, 3, 4}
时间复杂度: O(n)

std::lexicographical_compare

字典序比较

检查第一个范围是否在字典序上小于第二个范围。

std::string s1 = "apple";
std::string s2 = "banana";
bool result = std::lexicographical_compare(
  s1.begin(), s1.end(), s2.begin(), s2.end());
// result = true
时间复杂度: O(min(n, m))

std::equal_range

相等元素范围

在排序范围中查找等于给定值的子范围。

std::vector<int> v = {1, 2, 3, 3, 3, 4, 5};
auto [lower, upper] = std::equal_range(v.begin(), v.end(), 3);
std::cout << "3 的数量: " << std::distance(lower, upper);
// 输出: 3 的数量: 3
时间复杂度: O(log n)

std::inplace_merge

原地合并

合并两个已排序的子范围为一个排序范围。

std::vector<int> v = {1, 3, 5, 2, 4, 6};
auto mid = v.begin() + 3;
std::inplace_merge(v.begin(), mid, v.end());
// v 现在为 {1, 2, 3, 4, 5, 6}
时间复杂度: O(n)

std::is_heap

检查堆结构

检查范围是否形成堆结构。

std::vector<int> heap = {9, 5, 4, 1, 3, 2};
std::vector<int> not_heap = {1, 2, 3, 4, 5};
bool isHeap1 = std::is_heap(heap.begin(), heap.end()); // true
bool isHeap2 = std::is_heap(not_heap.begin(), not_heap.end()); // false
时间复杂度: O(n)

std::make_heap

创建堆

将范围转化为堆结构。

std::vector<int> v = {3, 1, 4, 1, 5, 9};
std::make_heap(v.begin(), v.end());
// v 现在为堆: {9, 5, 4, 1, 1, 3}
时间复杂度: O(n)

std::push_heap

向堆添加元素

将范围末尾的元素插入堆中。

std::vector<int> v = {9, 5, 4, 1, 1, 3}; // 堆
v.push_back(7);
std::push_heap(v.begin(), v.end());
// v 现在为 {9, 5, 7, 1, 1, 3, 4}
时间复杂度: O(log n)

std::pop_heap

从堆中移除最大元素

将堆的最大元素移动到范围末尾。

std::vector<int> v = {9, 5, 4, 1, 1, 3};
std::pop_heap(v.begin(), v.end());
int max = v.back();
v.pop_back();
// max = 9, v 现在为 {5, 3, 4, 1, 1}
时间复杂度: O(log n)

std::sort_heap

堆排序

将堆转换为排序范围。

std::vector<int> v = {9, 5, 4, 1, 1, 3};
std::make_heap(v.begin(), v.end());
std::sort_heap(v.begin(), v.end());
// v 现在为 {1, 1, 3, 4, 5, 9}
时间复杂度: O(n log n)

std::clamp

夹紧值

将值限制在给定的范围内。

int value = 15;
int clamped = std::clamp(value, 0, 10);
// clamped = 10

value = -5;
clamped = std::clamp(value, 0, 10);
// clamped = 0
时间复杂度: O(1)

std::sample

随机抽样

从范围中随机选择n个元素。

std::vector<int> population = {1, 2, 3, 4, 5, 6, 7, 8, 9};
std::vector<int> samples(3);
std::random_device rd;
std::mt19937 gen(rd());
std::sample(population.begin(), population.end(),
  samples.begin(), 3, gen);
// samples 可能为 {2, 5, 8}
时间复杂度: O(n)

std::reduce

并行累加

类似accumulate,但可并行执行。

std::vector<int> v = {1, 2, 3, 4, 5};
int sum = std::reduce(v.begin(), v.end(), 0);
// sum = 15

int product = std::reduce(v.begin(), v.end(), 1,
  [](int a, int b) { return a * b; });
// product = 120
时间复杂度: O(n)

std::exclusive_scan

排除式扫描

计算前缀和,每个结果不包含当前元素。

std::vector<int> v = {1, 2, 3, 4};
std::vector<int> result(4);
std::exclusive_scan(v.begin(), v.end(), result.begin(), 0);
// result 现在为 {0, 1, 3, 6}
时间复杂度: O(n)

std::transform_reduce

变换后累加

先应用变换,然后累加结果。

std::vector<int> v = {1, 2, 3, 4};
int sum_squares = std::transform_reduce(v.begin(), v.end(),
  0, std::plus<>(),
  [](int x) { return x * x; });
// sum_squares = 1+4+9+16 = 30
时间复杂度: O(n)

std::set_symmetric_difference

对称差集

计算两个排序范围的对称差集(在任一范围但不在两个范围中)。

std::vector<int> v1 = {1, 2, 3, 4};
std::vector<int> v2 = {3, 4, 5, 6};
std::vector<int> sym_diff;
std::set_symmetric_difference(v1.begin(), v1.end(),
  v2.begin(), v2.end(), std::back_inserter(sym_diff));
// sym_diff 现在为 {1, 2, 5, 6}
时间复杂度: O(2*(n+m))

std::is_permutation

检查排列

检查一个范围是否是另一个范围的排列。

std::string s1 = "abcd";
std::string s2 = "dcba";
bool result = std::is_permutation(s1.begin(), s1.end(), s2.begin());
// result = true
时间复杂度: O(n²)

std::next_permutation

下一个排列

将范围变换为下一个字典序排列。

std::vector<int> v = {1, 2, 3};
do {
  // 打印当前排列
} while (std::next_permutation(v.begin(), v.end()));
// 输出所有排列: 123, 132, 213, 231, 312, 321
时间复杂度: O(n)

std::prev_permutation

上一个排列

将范围变换为上一个字典序排列。

std::vector<int> v = {3, 2, 1};
do {
  // 打印当前排列
} while (std::prev_permutation(v.begin(), v.end()));
// 输出所有排列: 321, 312, 231, 213, 132, 123
时间复杂度: O(n)

std::partition_point

划分点查找

在划分范围中查找划分点。

std::vector<int> v = {2, 4, 6, 1, 3, 5}; // 已划分
auto it = std::partition_point(v.begin(), v.end(),
  [](int n) { return n % 2 == 0; });
std::cout << "划分点: " << *it; // 输出: 划分点: 1
时间复杂度: O(log n)

std::rotate_copy

旋转并复制

旋转范围并将结果复制到目标范围。

std::vector<int> src = {1, 2, 3, 4, 5};
std::vector<int> dest(5);
std::rotate_copy(src.begin(), src.begin() + 2,
  src.end(), dest.begin());
// dest 现在为 {3, 4, 5, 1, 2}
时间复杂度: O(n)

std::shift_left

左移元素

将范围中的元素向左移动。

std::vector<int> v = {1, 2, 3, 4, 5};
std::shift_left(v.begin(), v.end(), 2);
// v 现在为 {3, 4, 5, 4, 5} (最后两个元素未定义)
时间复杂度: O(n)

std::shift_right

右移元素

将范围中的元素向右移动。

std::vector<int> v = {1, 2, 3, 4, 5};
std::shift_right(v.begin(), v.end(), 2);
// v 现在为 {1, 2, 1, 2, 3} (前两个元素未定义)
时间复杂度: O(n)

std::gcd

最大公约数

计算两个整数的最大公约数。

int a = 60, b = 48;
int result = std::gcd(a, b);
// result = 12
时间复杂度: O(log(min(a,b)))

std::lcm

最小公倍数

计算两个整数的最小公倍数。

int a = 6, b = 8;
int result = std::lcm(a, b);
// result = 24
时间复杂度: O(log(min(a,b)))

std::adjacent_difference

相邻差计算

计算范围中相邻元素的差。

std::vector<int> v = {2, 4, 6, 8, 10};
std::vector<int> result(5);
std::adjacent_difference(v.begin(), v.end(), result.begin());
// result 现在为 {2, 2, 2, 2, 2}
时间复杂度: O(n)

std::search

子序列搜索

在范围中搜索子序列的出现。

std::string text = "the quick brown fox jumps over the lazy dog";
std::string pattern = "fox";
auto it = std::search(text.begin(), text.end(),
  pattern.begin(), pattern.end());
if (it != text.end()) {
  std::cout << "找到 'fox'";
}
时间复杂度: O(n*m)

std::search_n

重复元素搜索

在范围中搜索n个连续等于值的元素。

std::vector<int> v = {1, 2, 3, 3, 3, 4, 5};
auto it = std::search_n(v.begin(), v.end(), 3, 3);
if (it != v.end()) {
  std::cout << "找到3个连续的3";
}
时间复杂度: O(n)

std::find_first_of

查找首次出现

在范围中搜索另一个范围中的任意元素的首次出现。

std::string text = "Hello World";
std::string vowels = "aeiouAEIOU";
auto it = std::find_first_of(text.begin(), text.end(),
  vowels.begin(), vowels.end());
if (it != text.end()) {
  std::cout << "第一个元音: " << *it; // 'e'
}
时间复杂度: O(n*m)

std::copy_backward

向后复制

从范围的末尾开始复制元素。

std::vector<int> src = {1, 2, 3, 4, 5};
std::vector<int> dest(7);
std::copy_backward(src.begin(), src.end(), dest.end());
// dest 现在为 {0, 0, 1, 2, 3, 4, 5}
时间复杂度: O(n)

std::move

移动元素

将元素从一个范围移动到另一个范围。

std::vector<std::string> src = {"apple", "banana", "cherry"};
std::vector<std::string> dest(3);
std::move(src.begin(), src.end(), dest.begin());
// src 现在为空字符串,dest 包含原始字符串
时间复杂度: O(n)

std::move_backward

向后移动

从范围的末尾开始移动元素。

std::vector<std::string> src = {"apple", "banana", "cherry"};
std::vector<std::string> dest(5);
std::move_backward(src.begin(), src.end(), dest.end());
// dest: ["", "", "apple", "banana", "cherry"]
时间复杂度: O(n)

std::fill_n

填充n个元素

将值赋给范围的前n个元素。

std::vector<int> v(5); // {0, 0, 0, 0, 0}
std::fill_n(v.begin(), 3, 10);
// v 现在为 {10, 10, 10, 0, 0}
时间复杂度: O(n)

std::generate_n

生成n个元素

将函数生成的值赋给范围的前n个元素。

std::vector<int> v(5);
int n = 1;
std::generate_n(v.begin(), 3, [&n]() { return n++; });
// v 现在为 {1, 2, 3, 0, 0}
时间复杂度: O(n)

std::remove_copy

复制时移除

复制范围中的元素,但跳过等于特定值的元素。

std::vector<int> src = {1, 2, 3, 2, 4};
std::vector<int> dest;
std::remove_copy(src.begin(), src.end(),
  std::back_inserter(dest), 2);
// dest 现在为 {1, 3, 4}
时间复杂度: O(n)

std::replace_copy

复制时替换

复制范围中的元素,同时替换特定值。

std::vector<int> src = {1, 2, 3, 2, 4};
std::vector<int> dest;
std::replace_copy(src.begin(), src.end(),
  std::back_inserter(dest), 2, 9);
// dest 现在为 {1, 9, 3, 9, 4}
时间复杂度: O(n)

std::swap_ranges

交换范围

交换两个范围中的元素。

std::vector<int> v1 = {1, 2, 3, 4};
std::vector<int> v2 = {9, 8, 7, 6};
std::swap_ranges(v1.begin(), v1.end(), v2.begin());
// v1: {9, 8, 7, 6}, v2: {1, 2, 3, 4}
时间复杂度: O(n)

std::is_partitioned

检查划分

检查范围是否被划分为满足谓词的元素在前。

std::vector<int> v = {2, 4, 6, 1, 3, 5};
bool partitioned = std::is_partitioned(v.begin(), v.end(),
  [](int n) { return n % 2 == 0; });
// partitioned = true
时间复杂度: O(n)

std::stable_partition

稳定划分

划分范围,同时保持相等元素的相对顺序。

std::vector<int> v = {1, 2, 3, 4, 5, 6};
auto it = std::stable_partition(v.begin(), v.end(),
  [](int i) { return i % 2 == 0; });
// v: {2, 4, 6, 1, 3, 5} (保持原始顺序)
时间复杂度: O(n log n)

std::partial_sort_copy

部分排序并复制

对范围部分排序并将结果复制到目标范围。

std::vector<int> src = {5, 3, 1, 6, 4, 2};
std::vector<int> dest(3);
std::partial_sort_copy(src.begin(), src.end(),
  dest.begin(), dest.end());
// dest 现在为 {1, 2, 3}
时间复杂度: O(n log k)

std::is_sorted_until

查找排序结束点

查找范围中第一个破坏排序的元素。

std::vector<int> v = {1, 2, 3, 5, 4, 6};
auto it = std::is_sorted_until(v.begin(), v.end());
std::cout << "排序结束于: " << *it; // 输出: 4
时间复杂度: O(n)

std::partition_copy

划分并复制

将范围划分为两个范围,同时复制元素。

std::vector<int> src = {1, 2, 3, 4, 5, 6};
std::vector<int> even, odd;
std::partition_copy(src.begin(), src.end(),
  std::back_inserter(even), std::back_inserter(odd),
  [](int n) { return n % 2 == 0; });
// even: {2, 4, 6}, odd: {1, 3, 5}
时间复杂度: O(n)

std::midpoint

中点计算

计算两个数字的中点,避免溢出。

int a = 100, b = 200;
int mid = std::midpoint(a, b); // 150

float x = 1e10f, y = 1e20f;
float mid_float = std::midpoint(x, y); // 安全计算
时间复杂度: O(1)

std::lerp

线性插值

计算两个值之间的线性插值。

float a = 10.0f, b = 20.0f;
float interpolated = std::lerp(a, b, 0.5f); // 15.0f

// 动画应用:t从0到1变化
for (float t = 0; t <= 1; t += 0.1f) {
  float value = std::lerp(start, end, t);
}
时间复杂度: O(1)

std::ranges::sort

范围排序

C++20范围库中的排序算法。

#include <ranges>
#include <algorithm>

std::vector<int> v = {5, 3, 1, 4, 2};
std::ranges::sort(v);
// v 现在为 {1, 2, 3, 4, 5}

// 使用投影
std::vector<std::string> words = {"apple", "banana", "cherry"};
std::ranges::sort(words, {}, &std::string::size);
// 按长度排序: {"apple", "cherry", "banana"}
平均时间复杂度: O(n log n)

std::views::transform

变换视图

C++20范围库中的惰性变换操作。

#include <ranges>

std::vector<int> v = {1, 2, 3, 4, 5};
auto squared = v | std::views::transform([](int n) {
  return n * n;
});

for (int n : squared) {
  std::cout << n << " "; // 1, 4, 9, 16, 25
}
惰性求值,时间复杂度取决于使用

std::views::filter

过滤视图

C++20范围库中的惰性过滤操作。

std::vector<int> v = {1, 2, 3, 4, 5};
auto even = v | std::views::filter([](int n) {
  return n % 2 == 0;
});

for (int n : even) {
  std::cout << n << " "; // 2, 4
}
惰性求值,时间复杂度取决于使用

std::adjacent_difference

相邻差计算

计算范围中相邻元素的差。

std::vector v = {2, 4, 6, 8, 10};
std::vector result(5);
std::adjacent_difference(v.begin(), v.end(), result.begin());
// result: {2, 2, 2, 2, 2}
时间复杂度: O(n)

std::search

子序列搜索

在范围中搜索子序列的出现。

std::string text = "the quick brown fox jumps over the lazy dog";
std::string pattern = "fox";
auto it = std::search(text.begin(), text.end(),
  pattern.begin(), pattern.end());
// it指向"fox"的开头
时间复杂度: O(n*m)

std::search_n

重复元素搜索

在范围中搜索n个连续等于值的元素。

std::vector v = {1, 2, 3, 3, 3, 4, 5};
auto it = std::search_n(v.begin(), v.end(), 3, 3);
// it指向第一个3的位置
时间复杂度: O(n)

std::find_first_of

查找首次出现

在范围中搜索另一个范围中的任意元素的首次出现。

std::string text = "Hello World";
std::string vowels = "aeiouAEIOU";
auto it = std::find_first_of(text.begin(), text.end(),
  vowels.begin(), vowels.end());
// it指向'e'
时间复杂度: O(n*m)

std::copy_backward

向后复制

从范围的末尾开始复制元素。

std::vector src = {1, 2, 3, 4, 5};
std::vector dest(7);
std::copy_backward(src.begin(), src.end(), dest.end());
// dest: {0, 0, 1, 2, 3, 4, 5}
时间复杂度: O(n)

std::move

移动元素

将元素从一个范围移动到另一个范围。

std::vector src = {"apple", "banana", "cherry"};
std::vector dest(3);
std::move(src.begin(), src.end(), dest.begin());
// src现在为空字符串,dest包含原始字符串
时间复杂度: O(n)

std::move_backward

向后移动

从范围的末尾开始移动元素。

std::vector src = {"apple", "banana", "cherry"};
std::vector dest(5);
std::move_backward(src.begin(), src.end(), dest.end());
// dest: ["", "", "apple", "banana", "cherry"]
时间复杂度: O(n)

std::fill_n

填充n个元素

将值赋给范围的前n个元素。

std::vector v(5); // {0, 0, 0, 0, 0}
std::fill_n(v.begin(), 3, 10);
// v: {10, 10, 10, 0, 0}
时间复杂度: O(n)

std::generate_n

生成n个元素

将函数生成的值赋给范围的前n个元素。

std::vector v(5);
int n = 1;
std::generate_n(v.begin(), 3, [&n]() { return n++; });
// v: {1, 2, 3, 0, 0}
时间复杂度: O(n)

std::remove_copy

复制时移除

复制范围中的元素,但跳过等于特定值的元素。

std::vector src = {1, 2, 3, 2, 4};
std::vector dest;
std::remove_copy(src.begin(), src.end(),
  std::back_inserter(dest), 2);
// dest: {1, 3, 4}
时间复杂度: O(n)

std::replace_copy

复制时替换

复制范围中的元素,同时替换特定值。

std::vector src = {1, 2, 3, 2, 4};
std::vector dest;
std::replace_copy(src.begin(), src.end(),
  std::back_inserter(dest), 2, 9);
// dest: {1, 9, 3, 9, 4}
时间复杂度: O(n)

std::swap_ranges

交换范围

交换两个范围中的元素。

std::vector v1 = {1, 2, 3, 4};
std::vector v2 = {9, 8, 7, 6};
std::swap_ranges(v1.begin(), v1.end(), v2.begin());
// v1: {9, 8, 7, 6}, v2: {1, 2, 3, 4}
时间复杂度: O(n)

std::is_partitioned

检查划分

检查范围是否被划分为满足谓词的元素在前。

std::vector v = {2, 4, 6, 1, 3, 5};
bool partitioned = std::is_partitioned(v.begin(), v.end(),
  [](int n) { return n % 2 == 0; });
// partitioned = true
时间复杂度: O(n)

std::stable_partition

稳定划分

划分范围,同时保持相等元素的相对顺序。

std::vector v = {1, 2, 3, 4, 5, 6};
auto it = std::stable_partition(v.begin(), v.end(),
  [](int i) { return i % 2 == 0; });
// v: {2, 4, 6, 1, 3, 5} (保持原始顺序)
时间复杂度: O(n log n)

std::partial_sort_copy

部分排序并复制

对范围部分排序并将结果复制到目标范围。

std::vector src = {5, 3, 1, 6, 4, 2};
std::vector dest(3);
std::partial_sort_copy(src.begin(), src.end(),
  dest.begin(), dest.end());
// dest: {1, 2, 3}
时间复杂度: O(n log k)

std::is_sorted_until

查找排序结束点

查找范围中第一个破坏排序的元素。

std::vector v = {1, 2, 3, 5, 4, 6};
auto it = std::is_sorted_until(v.begin(), v.end());
// *it = 4
时间复杂度: O(n)

std::partition_copy

划分并复制

将范围划分为两个范围,同时复制元素。

std::vector src = {1, 2, 3, 4, 5, 6};
std::vector even, odd;
std::partition_copy(src.begin(), src.end(),
  std::back_inserter(even), std::back_inserter(odd),
  [](int n) { return n % 2 == 0; });
// even: {2, 4, 6}, odd: {1, 3, 5}
时间复杂度: O(n)

std::midpoint

中点计算

计算两个数字的中点,避免溢出。

int a = 100, b = 200;
int mid = std::midpoint(a, b); // 150

float x = 1e10f, y = 1e20f;
float mid_float = std::midpoint(x, y); // 安全计算
时间复杂度: O(1)

std::lerp

线性插值

计算两个值之间的线性插值。

float a = 10.0f, b = 20.0f;
float interpolated = std::lerp(a, b, 0.5f); // 15.0f

// 动画应用:t从0到1变化
for (float t = 0; t <= 1; t += 0.1f) {
  float value = std::lerp(start, end, t);
}
时间复杂度: O(1)

std::ranges::sort

范围排序

C++20范围库中的排序算法。

#include <ranges>
#include <algorithm>

std::vector v = {5, 3, 1, 4, 2};
std::ranges::sort(v);
// v: {1, 2, 3, 4, 5}

// 使用投影
std::vector words = {"apple", "banana", "cherry"};
std::ranges::sort(words, {}, &std::string::size);
// 按长度排序: {"apple", "cherry", "banana"}
平均时间复杂度: O(n log n)

std::views::transform

变换视图

C++20范围库中的惰性变换操作。

#include <ranges>

std::vector v = {1, 2, 3, 4, 5};
auto squared = v | std::views::transform([](int n) {
  return n * n;
});

for (int n : squared) {
  std::cout << n << " "; // 1, 4, 9, 16, 25
}
惰性求值,时间复杂度取决于使用

std::views::filter

过滤视图

C++20范围库中的惰性过滤操作。

std::vector v = {1, 2, 3, 4, 5};
auto even = v | std::views::filter([](int n) {
  return n % 2 == 0;
});

for (int n : even) {
  std::cout << n << " "; // 2, 4
}
惰性求值,时间复杂度取决于使用

std::exclusive_scan

排除式扫描

计算前缀和,每个结果不包含当前元素。

std::vector<int> v = {1, 2, 3, 4};
std::vector<int> result(4);
std::exclusive_scan(v.begin(), v.end(), result.begin(), 0);
// result: {0, 1, 3, 6}
时间复杂度: O(n)

std::inclusive_scan

包含式扫描

计算前缀和,每个结果包含当前元素。

std::vector<int> v = {1, 2, 3, 4};
std::vector<int> result(4);
std::inclusive_scan(v.begin(), v.end(), result.begin());
// result: {1, 3, 6, 10}
时间复杂度: O(n)

std::transform_exclusive_scan

变换后排除式扫描

先变换元素,然后计算排除式扫描。

std::vector<int> v = {1, 2, 3, 4};
std::vector<int> result(4);
std::transform_exclusive_scan(v.begin(), v.end(), result.begin(), 0,
  std::plus<>(), [](int x) { return x * x; });
// result: {0, 1, 5, 14}
时间复杂度: O(n)

std::transform_inclusive_scan

变换后包含式扫描

先变换元素,然后计算包含式扫描。

std::vector<int> v = {1, 2, 3, 4};
std::vector<int> result(4);
std::transform_inclusive_scan(v.begin(), v.end(), result.begin(),
  std::plus<>(), [](int x) { return x * x; });
// result: {1, 5, 14, 30}
时间复杂度: O(n)

std::sample

随机抽样

从范围中随机选择n个元素。

std::vector<int> population = {1, 2, 3, 4, 5, 6, 7, 8, 9};
std::vector<int> samples(3);
std::random_device rd;
std::mt19937 gen(rd());
std::sample(population.begin(), population.end(),
  samples.begin(), 3, gen);
// samples 可能为 {2, 5, 8}
时间复杂度: O(n)

std::reduce

并行累加

类似accumulate,但可并行执行。

std::vector<int> v = {1, 2, 3, 4, 5};
int sum = std::reduce(v.begin(), v.end()); // 15
int product = std::reduce(v.begin(), v.end(), 1,
  [](int a, int b) { return a * b; }); // 120
时间复杂度: O(n)

std::transform_reduce

变换后累加

先应用变换,然后累加结果。

std::vector<int> v = {1, 2, 3, 4};
int sum_squares = std::transform_reduce(v.begin(), v.end(),
  0, std::plus<>(),
  [](int x) { return x * x; });
// sum_squares = 1+4+9+16 = 30
时间复杂度: O(n)

std::shift_left

左移元素

将范围中的元素向左移动。

std::vector<int> v = {1, 2, 3, 4, 5};
std::shift_left(v.begin(), v.end(), 2);
// v: {3, 4, 5, 4, 5} (最后两个元素未定义)
时间复杂度: O(n)

std::shift_right

右移元素

将范围中的元素向右移动。

std::vector<int> v = {1, 2, 3, 4, 5};
std::shift_right(v.begin(), v.end(), 2);
// v: {1, 2, 1, 2, 3} (前两个元素未定义)
时间复杂度: O(n)

std::is_sorted_until

查找排序结束点

查找范围中第一个破坏排序的元素。

std::vector<int> v = {1, 2, 3, 5, 4, 6};
auto it = std::is_sorted_until(v.begin(), v.end());
// *it = 4
时间复杂度: O(n)

std::is_heap_until

查找堆结束点

查找范围中第一个破坏堆结构的元素。

std::vector<int> v = {9, 5, 4, 2, 3, 1, 6}; // 6破坏了堆
auto it = std::is_heap_until(v.begin(), v.end());
// it指向6
时间复杂度: O(n)

std::stable_sort

稳定排序

对范围进行排序,同时保持相等元素的相对顺序。

std::vector<std::pair<int, char>> v = {
  {2, 'b'}, {1, 'a'}, {2, 'c'}, {1, 'd'}};
std::stable_sort(v.begin(), v.end(),
  [](auto a, auto b) { return a.first < b.first; });
// 排序后: {1, 'a'}, {1, 'd'}, {2, 'b'}, {2, 'c'}
时间复杂度: O(n log² n)

std::inplace_merge

原地合并

合并两个已排序的子范围为一个排序范围。

std::vector<int> v = {1, 3, 5, 2, 4, 6};
auto mid = v.begin() + 3;
std::inplace_merge(v.begin(), mid, v.end());
// v: {1, 2, 3, 4, 5, 6}
时间复杂度: O(n)

std::partition_point

划分点查找

在划分范围中查找划分点。

std::vector<int> v = {2, 4, 6, 1, 3, 5}; // 已划分
auto it = std::partition_point(v.begin(), v.end(),
  [](int n) { return n % 2 == 0; });
std::cout << "划分点: " << *it; // 输出: 划分点: 1
时间复杂度: O(log n)

std::rotate_copy

旋转并复制

旋转范围并将结果复制到目标范围。

std::vector<int> src = {1, 2, 3, 4, 5};
std::vector<int> dest(5);
std::rotate_copy(src.begin(), src.begin() + 2,
  src.end(), dest.begin());
// dest: {3, 4, 5, 1, 2}
时间复杂度: O(n)

std::unique_copy

复制唯一元素

从范围中复制元素,忽略连续的重复项。

std::vector<int> src = {1, 1, 2, 2, 3, 3, 3, 4};
std::vector<int> dest;
std::unique_copy(src.begin(), src.end(),
  std::back_inserter(dest));
// dest: {1, 2, 3, 4}
时间复杂度: O(n)

std::remove_copy

复制时移除

复制范围中的元素,但跳过等于特定值的元素。

std::vector<int> src = {1, 2, 3, 2, 4};
std::vector<int> dest;
std::remove_copy(src.begin(), src.end(),
  std::back_inserter(dest), 2);
// dest: {1, 3, 4}
时间复杂度: O(n)

std::replace_copy

复制时替换

复制范围中的元素,同时替换特定值。

std::vector<int> src = {1, 2, 3, 2, 4};
std::vector<int> dest;
std::replace_copy(src.begin(), src.end(),
  std::back_inserter(dest), 2, 9);
// dest: {1, 9, 3, 9, 4}
时间复杂度: O(n)

std::partition_copy

划分并复制

将范围划分为两个范围,同时复制元素。

std::vector<int> src = {1, 2, 3, 4, 5, 6};
std::vector<int> even, odd;
std::partition_copy(src.begin(), src.end(),
  std::back_inserter(even), std::back_inserter(odd),
  [](int n) { return n % 2 == 0; });
// even: {2, 4, 6}, odd: {1, 3, 5}
时间复杂度: O(n)

std::ranges::find

范围查找

C++20范围库中的查找算法。

#include <ranges>
#include <algorithm>

std::vector<int> v = {1, 2, 3, 4, 5};
auto it = std::ranges::find(v, 3);
// *it = 3
时间复杂度: O(n)

std::ranges::all_of

范围条件检查

C++20范围库中的条件检查算法。

std::vector<int> v = {2, 4, 6, 8};
bool allEven = std::ranges::all_of(v,
  [](int i) { return i % 2 == 0; });
// allEven = true
时间复杂度: O(n)

std::ranges::count_if

范围条件计数

C++20范围库中的条件计数算法。

std::vector<int> v = {1, 2, 3, 4, 5};
int count = std::ranges::count_if(v,
  [](int n) { return n > 3; });
// count = 2
时间复杂度: O(n)

std::views::iota

整数序列视图

C++20范围库中的惰性整数序列生成器。

for (int i : std::views::iota(1, 6)) {
  std::cout << i << " "; // 1 2 3 4 5
}

// 无限序列
auto infinite = std::views::iota(1);
for (int i : infinite | std::views::take(5)) {
  std::cout << i << " "; // 1 2 3 4 5
}
惰性求值,时间复杂度取决于使用

std::ranges::contains

范围包含检查

C++23新增算法,检查范围是否包含特定值。

#include <ranges>

std::vector<int> v = {1, 2, 3, 4, 5};
bool has3 = std::ranges::contains(v, 3); // true
bool has6 = std::ranges::contains(v, 6); // false
时间复杂度: O(n)

std::ranges::starts_with

范围前缀检查

C++23新增算法,检查范围是否以特定序列开头。

std::vector<int> v1 = {1, 2, 3, 4, 5};
std::vector<int> prefix = {1, 2};
bool starts = std::ranges::starts_with(v1, prefix); // true
时间复杂度: O(min(n, m))

std::ranges::ends_with

范围后缀检查

C++23新增算法,检查范围是否以特定序列结尾。

std::vector<int> v1 = {1, 2, 3, 4, 5};
std::vector<int> suffix = {4, 5};
bool ends = std::ranges::ends_with(v1, suffix); // true
时间复杂度: O(min(n, m))

std::ranges::fold_left

左折叠操作

C++23新增算法,对范围元素执行左折叠操作。

std::vector<int> v = {1, 2, 3, 4};
int sum = std::ranges::fold_left(v, 0, std::plus{}); // 10
int product = std::ranges::fold_left(v, 1, std::multiplies{}); // 24
时间复杂度: O(n)

std::ranges::fold_left_first

左折叠首元素

C++23新增算法,使用首元素作为初始值执行左折叠。

std::vector<int> v = {1, 2, 3, 4};
auto max = std::ranges::fold_left_first(v, std::greater{}); // 4
auto min = std::ranges::fold_left_first(v, std::less{}); // 1
时间复杂度: O(n)

std::ranges::fold_right

右折叠操作

C++23新增算法,对范围元素执行右折叠操作。

std::vector<int> v = {1, 2, 3, 4};
int result = std::ranges::fold_right(v, 0, [](int a, int b) {
  return a - b;
}); // 1 - (2 - (3 - (4 - 0))) = 1 - 2 + 3 - 4 = -2
时间复杂度: O(n)

std::ranges::chunk_by

条件分组视图

C++23新增视图,根据谓词将元素分组。

std::vector<int> v = {1, 1, 2, 3, 3, 3, 2, 2};
auto chunks = v | std::views::chunk_by(std::equal_to{});
for (auto chunk : chunks) {
  // [1,1], [2], [3,3,3], [2,2]
}
惰性求值,时间复杂度取决于使用

std::ranges::slide

滑动窗口视图

C++23新增视图,创建滑动窗口。

std::vector<int> v = {1, 2, 3, 4, 5};
auto windows = v | std::views::slide(3);
for (auto win : windows) {
  // [1,2,3], [2,3,4], [3,4,5]
}
惰性求值,时间复杂度取决于使用

std::ranges::join_with

分隔符连接视图

C++23新增视图,使用分隔符连接范围。

std::vector<std::vector<int>> v = {{1}, {2,3}, {4,5,6}};
auto joined = v | std::views::join_with(0);
// 生成序列: 1,0,2,3,0,4,5,6
惰性求值,时间复杂度取决于使用

std::ranges::zip_transform

变换压缩视图

C++23新增视图,对多个范围进行压缩和变换。

std::vector<int> a = {1, 2, 3};
std::vector<int> b = {4, 5, 6};
auto sums = std::views::zip_transform(std::plus{}, a, b);
// 生成序列: 5,7,9
惰性求值,时间复杂度取决于使用

std::ranges::adjacent

相邻元素视图

C++23新增视图,创建相邻元素组。

std::vector<int> v = {1, 2, 3, 4};
auto pairs = v | std::views::adjacent<2>;
for (auto [a, b] : pairs) {
  // (1,2), (2,3), (3,4)
}
惰性求值,时间复杂度取决于使用

std::ranges::cartesian_product

笛卡尔积视图

C++23新增视图,创建多个范围的笛卡尔积。

std::vector<int> x = {1, 2};
std::vector<char> y = {'a', 'b'};
auto product = std::views::cartesian_product(x, y);
for (auto [a, b] : product) {
  // (1,'a'), (1,'b'), (2,'a'), (2,'b')
}
惰性求值,时间复杂度取决于使用

std::ranges::find_last

查找最后出现

C++23新增算法,查找范围中最后一次出现的位置。

std::vector<int> v = {1, 2, 3, 2, 1};
auto result = std::ranges::find_last(v, 2);
if (result != v.end()) {
  std::cout << "最后出现位置: " << std::distance(v.begin(), result); // 3
}
时间复杂度: O(n)

std::ranges::find_last_if

条件查找最后出现

C++23新增算法,查找范围中最后一次满足条件的元素。

std::vector<int> v = {1, 4, 3, 6, 5};
auto result = std::ranges::find_last_if(v, [](int i) {
  return i % 2 == 0; // 查找最后一个偶数
});
// *result = 6
时间复杂度: O(n)

std::ranges::contains_subrange

子范围包含检查

C++23新增算法,检查范围是否包含特定子范围。

std::vector<int> v = {1, 2, 3, 4, 5};
std::vector<int> sub = {3, 4};
bool contains = std::ranges::contains_subrange(v, sub); // true
时间复杂度: O(n*m)

std::ranges::shift_left

范围左移

C++23新增算法,对范围执行左移操作。

std::vector<int> v = {1, 2, 3, 4, 5};
auto end = std::ranges::shift_left(v, 2);
// v: {3, 4, 5, 4, 5},end指向第3个元素
时间复杂度: O(n)

std::ranges::shift_right

范围右移

C++23新增算法,对范围执行右移操作。

C++23新增算法,对范围执行右移操作。

std::vector<int> v = {1, 2, 3, 4, 5};
auto end = std::ranges::shift_right(v, 2);
// v: {1, 2, 1, 2, 3},end指向第3个元素
时间复杂度: O(n)

std::ranges::iota

范围递增填充

C++23新增算法,用连续递增的值填充范围。

std::vector<int> v(5);
std::ranges::iota(v, 10);
// v: {10, 11, 12, 13, 14}
时间复杂度: O(n)

std::views::chunk

固定大小分组视图

C++23新增视图,将范围划分为固定大小的块。

std::vector<int> v = {1, 2, 3, 4, 5, 6};
auto chunks = v | std::views::chunk(2);
for (auto chunk : chunks) {
  // [1,2], [3,4], [5,6]
}
惰性求值,时间复杂度取决于使用

std::views::stride

步长视图

C++23新增视图,以指定步长遍历范围。

std::vector<int> v = {1, 2, 3, 4, 5, 6};
auto strided = v | std::views::stride(2);
for (int i : strided) {
  // 1, 3, 5
}
惰性求值,时间复杂度取决于使用

std::views::repeat

重复值视图

C++23新增视图,生成无限重复的值序列。

auto ones = std::views::repeat(1);
for (int i : ones | std::views::take(5)) {
  // 1, 1, 1, 1, 1
}
惰性求值,时间复杂度取决于使用

std::views::generate

生成视图

C++23新增视图,通过生成函数创建无限序列。

int count = 0;
auto seq = std::views::generate([&count] { return count++; });
for (int i : seq | std::views::take(5)) {
  // 0, 1, 2, 3, 4
}
惰性求值,时间复杂度取决于使用

std::views::enumerate

枚举视图

C++23新增视图,为范围元素添加索引。

std::vector<char> v = {'a', 'b', 'c'};
for (auto [index, value] : v | std::views::enumerate) {
  // (0,'a'), (1,'b'), (2,'c')
}
惰性求值,时间复杂度取决于使用

std::views::as_const

常量视图

C++23新增视图,将范围元素视为常量。

std::vector<int> v = {1, 2, 3};
auto const_view = v | std::views::as_const;
// 所有元素都是const int&
惰性求值,时间复杂度取决于使用

std::ranges::find_min

查找最小值

C++26提案算法,查找范围中的最小值。

std::vector<int> v = {5, 3, 8, 1, 6};
auto min = std::ranges::find_min(v);
// min = 1
时间复杂度: O(n)

std::ranges::find_max

查找最大值

C++26提案算法,查找范围中的最大值。

std::vector<int> v = {5, 3, 8, 1, 6};
auto max = std::ranges::find_max(v);
// max = 8
时间复杂度: O(n)

std::ranges::find_minmax

查找最小值和最大值

C++26提案算法,同时查找范围中的最小值和最大值。

std::vector<int> v = {5, 3, 8, 1, 6};
auto [min, max] = std::ranges::find_minmax(v);
// min = 1, max = 8
时间复杂度: O(n)

std::ranges::contains_if

条件包含检查

C++26提案算法,检查范围是否包含满足条件的元素。

std::vector<int> v = {1, 3, 5, 7, 9};
bool hasEven = std::ranges::contains_if(v,
  [](int i) { return i % 2 == 0; }); // false
时间复杂度: O(n)

std::ranges::contains_range

范围包含检查

C++26提案算法,检查范围是否包含特定子范围。

std::vector<int> v = {1, 2, 3, 4, 5};
std::vector<int> sub = {3, 4};
bool contains = std::ranges::contains_range(v, sub); // true
时间复杂度: O(n*m)

std::ranges::is_palindrome

回文检查

C++26提案算法,检查范围是否是回文序列。

std::string s1 = "racecar";
bool isPal1 = std::ranges::is_palindrome(s1); // true

std::string s2 = "hello";
bool isPal2 = std::ranges::is_palindrome(s2); // false
时间复杂度: O(n)

std::ranges::rotate_copy

旋转并复制

C++26提案算法,旋转范围并将结果复制到目标范围。

std::vector<int> src = {1, 2, 3, 4, 5};
std::vector<int> dest(5);
std::ranges::rotate_copy(src, dest.begin(), src.begin() + 2);
// dest: {3, 4, 5, 1, 2}
时间复杂度: O(n)

std::ranges::shift

通用移位

C++26提案算法,根据偏移量左移或右移范围。

std::vector<int> v = {1, 2, 3, 4, 5};
std::ranges::shift(v, 2); // 左移2位
// v: {3, 4, 5, 4, 5}

std::ranges::shift(v, -2); // 右移2位
// v: {1, 2, 1, 2, 3}
时间复杂度: O(n)

std::views::pairwise

相邻对视图

C++26提案视图,创建相邻元素对。

std::vector<int> v = {1, 2, 3, 4};
for (auto [a, b] : v | std::views::pairwise) {
  // (1,2), (2,3), (3,4)
}
惰性求值,时间复杂度取决于使用

std::views::slide_by

条件滑动窗口

C++26提案视图,根据谓词创建滑动窗口。

std::vector<int> v = {1, 2, 4, 5, 7, 8};
auto windows = v | std::views::slide_by([](int a, int b) {
  return b - a == 1; // 连续数字分组
});
for (auto win : windows) {
  // [1,2], [4,5], [7,8]
}
惰性求值,时间复杂度取决于使用

std::views::zip_transform

变换压缩视图

C++26提案视图,对多个范围进行压缩和变换。

std::vector<int> a = {1, 2, 3};
std::vector<char> b = {'a', 'b', 'c'};
auto combined = std::views::zip_transform(
  [](int i, char c) { return std::to_string(i) + c; }, a, b);
// "1a", "2b", "3c"
惰性求值,时间复杂度取决于使用

std::ranges::to

范围转换

C++23范围适配器,将范围转换为容器。

auto squares = std::views::iota(1, 5)
  | std::views::transform([](int i) { return i * i; });
auto vec = squares | std::ranges::to<std::vector<int>>();
// vec: {1, 4, 9, 16}
时间复杂度: O(n)