std::all_of, std::any_of, std::none_of - cppreference.com
来自cppreference.com
| 在标头 |
||
template< class InputIt, class UnaryPred > bool all_of( InputIt first, InputIt last, UnaryPred p ); |
(1) | (C++11 起) (C++20 起为 constexpr) |
template< class InputIt, class UnaryPred > bool any_of( InputIt first, InputIt last, UnaryPred p ); |
(2) | (C++11 起) (C++20 起为 constexpr) |
template< class InputIt, class UnaryPred > bool none_of( InputIt first, InputIt last, UnaryPred p ); |
(3) | (C++11 起) (C++20 起为 constexpr) |
template< class ExecutionPolicy, class ForwardIt, class UnaryPred > bool all_of( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, UnaryPred p ); |
(4) | (C++17 起) |
template< class ExecutionPolicy, class ForwardIt, class UnaryPred > bool any_of( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, UnaryPred p ); |
(5) | (C++17 起) |
template< class ExecutionPolicy, class ForwardIt, class UnaryPred > bool none_of( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, UnaryPred p ); |
(6) | (C++17 起) |
1) 检查一元谓词 p 是否对范围 [first, last) 中所有元素返回 true。
2) 检查一元谓词 p 是否对范围 [first, last) 中至少一个元素返回 true。
3) 检查一元谓词 p 是否不对范围 [first, last) 中任何元素返回 true。
4-6) 同 (1-3),但按照 policy 执行。
这些重载只有在以下表达式的值是 true 时才会参与重载决议:
|
|
(C++20 前) |
|
|
(C++20 起) |
参数
| first, last | - | 表示要检验的元素范围的迭代器对 |
| policy | - | 所用的执行策略 |
| p | - | 一元谓词。
对每个(可为 const 的) |
| 类型要求 | ||
-InputIt 必须满足老式输入迭代器 (LegacyInputIterator) 。
| ||
-ForwardIt 必须满足老式向前迭代器 (LegacyForwardIterator) 。
| ||
-UnaryPred 必须满足谓词 (Predicate) 。
| ||
返回值
范围中含有 true 元素
|
是 | 否 | ||
|---|---|---|---|---|
范围中含有 false 元素
|
是 | 否 | 是 | 否[1] |
all_of
|
false
|
true
|
false
|
true
|
any_of
|
true
|
true
|
false
|
false
|
none_of
|
false
|
false
|
true
|
true
|
- ↑ 此情况下范围为空。
复杂度
1-6) 应用最多 std::distance(first, last) 次谓词 p。
异常
注解
| 功能特性测试宏 | 值 | 标准 | 功能特性 |
|---|---|---|---|
__cpp_lib_parallel_algorithm |
202506L |
(C++26) | 并行范围算法,(4-6) |
可能的实现
参阅
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"; using namespace std::placeholders; if (std::none_of(v.cbegin(), v.cend(), std::bind(std::modulus<>(), _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 整除