enumerate

<algorithm>

any_of

检查一个整数数组中是否存在偶数。

bool ans = std::any_of(A.begin(), A.end(), [](int x) { return x % 2 == 0; });

all_of

检查一个整数数组中的所有元素是否都大于 \(0\)

bool ans = std::all_of(A.begin(), A.end(), [](int x) { return x > 0; });

none_of

检查一个整数数组中是否没有任何元素是负数。

bool ans = std::none_of(A.begin(), A.end(), [](int x) { return x < 0; });

find_if

在一个整数数组中,查找第一个偶数。

auto it = std::find_if(A.begin(), A.end(), [](int x) { return x % 2 == 0; });

max_element

找到一个整数数组中的最大值。

auto it = std::max_element(A.begin(), A.end());

min_element

找到一个整数数组中的最小值。

auto it = std::min_element(A.begin(), A.end());

<numeric>

accumulate

计算一个整数数组的和。

int sum = std::accumulate(A.begin(), A.end(), 0);

计算所有元素的乘积。

int product = std::accumulate(A.begin(), A.end(), 1, [](int a, int b) { return a * b; });

partial_sum

前缀和:

std::partial_sum(A.begin(), A.end(), prefix.begin() + 1);

前缀异或和:

std::partial_sum(A.begin(), A.end(), prefix.begin() + 1, [](int a, int b) { return a ^ b; });

后缀和:

std::partial_sum(A.rbegin(), A.rend(), suffix.rbegin() + 1);
posted @ 2025-06-05 12:33  Lucius7  阅读(13)  评论(0)    收藏  举报