std::all_of, std::any_of, std::none_of
来自cppreference.com
在标头 <algorithm> 定义
|
||
template< class InputIt, class UnaryPred > bool all_of( InputIt first, InputIt last, UnaryPred p ); |
(1) | (C++11 起) (C++20 起为 constexpr ) |
template< class ExecutionPolicy, class ForwardIt, class UnaryPred > bool all_of( ExecutionPolicy&& policy, |
(2) | (C++17 起) |
template< class InputIt, class UnaryPred > bool any_of( InputIt first, InputIt last, UnaryPred p ); |
(3) | (C++11 起) (C++20 起为 constexpr ) |
template< class ExecutionPolicy, class ForwardIt, class UnaryPred > bool any_of( ExecutionPolicy&& policy, |
(4) | (C++17 起) |
template< class InputIt, class UnaryPred > bool none_of( InputIt first, InputIt last, UnaryPred p ); |
(5) | (C++11 起) (C++20 起为 constexpr ) |
template< class ExecutionPolicy, class ForwardIt, class UnaryPred > bool none_of( ExecutionPolicy&& policy, |
(6) | (C++17 起) |
1) 检查一元谓词 p 是否对范围
[
first,
last)
中所有元素返回 true。3) 检查一元谓词 p 是否对范围
[
first,
last)
中至少一个元素返回 true。5) 检查一元谓词 p 是否不对范围
[
first,
last)
中任何元素返回 true。2,4,6) 同 (1,3,5),但按照 policy 执行。这些重载只有在
是 true 时时才会参与重载决议。
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> |
(C++20 前) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> |
(C++20 起) |
参数
first, last | - | 要检验的元素范围 |
policy | - | 所用的执行策略。细节见执行策略。 |
p | - | 一元谓词。 对每个(可为 const 的) |
类型要求 | ||
-InputIt 必须满足老式输入迭代器 (LegacyInputIterator) 。
| ||
-ForwardIt 必须满足老式向前迭代器 (LegacyForwardIterator) 。
| ||
-UnaryPred 必须满足谓词 (Predicate) 。
|
返回值
是否有 true 元素 | 是 | 否 | ||
---|---|---|---|---|
是否有 false 元素 | 是 | 否 | 是 | 否 |
all_of
|
false | true | false | true |
any_of
|
true | true | false | false |
none_of
|
false | false | true | true |
复杂度
异常
拥有名为 ExecutionPolicy
的模板形参的重载按下列方式报告错误:
- 如果作为算法一部分调用的函数的执行抛出异常,且
ExecutionPolicy
是标准策略之一,那么调用 std::terminate。对于任何其他ExecutionPolicy
,行为由实现定义。 - 如果算法无法分配内存,那么抛出 std::bad_alloc。
可能的实现
参阅
-
all_of
在 libstdc++ 与 libc++ 中的实现。 -
any_of
在 libstdc++ 与 libc++ 中的实现。 -
none_of
在 libstdc++ 与 libc++ 中的实现。
all_of |
---|
template<class InputIt, class UnaryPred> constexpr bool all_of(InputIt first, InputIt last, UnaryPred p) { return std::find_if_not(first, last, p) == last; } |
any_of |
template<class InputIt, class UnaryPred> constexpr bool any_of(InputIt first, InputIt last, UnaryPred p) { return std::find_if(first, last, p) != last; } |
none_of |
template<class InputIt, class UnaryPred> constexpr bool none_of(InputIt first, InputIt last, UnaryPred p) { return std::find_if(first, last, p) == last; } |
示例
运行此代码
#include <algorithm> #include <functional> #include <iostream> #include <iterator> #include <numeric> #include <vector> int main() { std::vector<int> v(10, 2); std::partial_sum(v.cbegin(), v.cend(), v.begin()); std::cout << "在这些数字中:"; std::copy(v.cbegin(), v.cend(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; if (std::all_of(v.cbegin(), v.cend(), [](int i) { return i % 2 == 0; })) std::cout << "所有数字都是偶数\n"; if (std::none_of(v.cbegin(), v.cend(), std::bind(std::modulus<>(), std::placeholders::_1, 2))) std::cout << "没有任何数字是奇数\n"; struct DivisibleBy { const int d; DivisibleBy(int n) : d(n) {} bool operator()(int n) const { return n % d == 0; } }; if (std::any_of(v.cbegin(), v.cend(), DivisibleBy(7))) std::cout << "至少有一个数字可以被 7 整除\n"; }
输出:
在这些数字中:2 4 6 8 10 12 14 16 18 20 所有数字都是偶数 没有任何数字是奇数 至少有一个数字可以被 7 整除
参阅
(C++20)(C++20)(C++20) |
检查谓词是否对范围中所有、任一或无元素为 true (niebloid) |