std::all_of、std::any_of、std::none_of
来自 cppreference.cn
定义于头文件 <algorithm> |
||
template< class InputIt, class UnaryPred > bool all_of( InputIt first, InputIt last, UnaryPred p ); |
(1) | (since C++11) (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt, class UnaryPred > bool all_of( ExecutionPolicy&& policy, |
(2) | (since C++17) |
template< class InputIt, class UnaryPred > bool any_of( InputIt first, InputIt last, UnaryPred p ); |
(3) | (since C++11) (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt, class UnaryPred > bool any_of( ExecutionPolicy&& policy, |
(4) | (since C++17) |
template< class InputIt, class UnaryPred > bool none_of( InputIt first, InputIt last, UnaryPred p ); |
(5) | (since C++11) (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt, class UnaryPred > bool none_of( ExecutionPolicy&& policy, |
(6) | (since C++17) |
1) 检查一元谓词 p 是否对范围
[
first,
last)
内的至少一个元素返回 false。3) 检查一元谓词 p 是否对范围
[
first,
last)
内的至少一个元素返回 true。5) 检查一元谓词 p 是否对范围
[
first,
last)
内的所有元素都不返回 true。2,4,6) 与 (1,3,5) 相同,但根据 policy 执行。
这些重载仅在满足以下所有条件时才参与重载解析
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 为 true。 |
(until C++20) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> 为 true。 |
(since C++20) |
目录 |
[编辑] 参数
first, last | - | 定义要检查的元素范围的迭代器对 |
policy | - | 要使用的执行策略 |
p | - | 一元谓词。 表达式 p(v) 对于类型为(可能是 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 |
- ↑ 在这种情况下,范围为空。
[编辑] 复杂度
[编辑] 异常
具有名为 ExecutionPolicy
的模板参数的重载会按如下方式报告错误
- 如果作为算法一部分调用的函数的执行抛出异常,并且
ExecutionPolicy
是标准策略之一,则调用 std::terminate。对于任何其他ExecutionPolicy
,行为是实现定义的。 - 如果算法无法分配内存,则抛出 std::bad_alloc。
[编辑] 可能的实现
另请参见以下各项的实现:
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 << "Among the numbers: "; 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 << "All numbers are even\n"; if (std::none_of(v.cbegin(), v.cend(), std::bind(std::modulus<>(), std::placeholders::_1, 2))) std::cout << "None of them are odd\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 << "At least one number is divisible by 7\n"; }
输出
Among the numbers: 2 4 6 8 10 12 14 16 18 20 All numbers are even None of them are odd At least one number is divisible by 7
[编辑] 参见
(C++20)(C++20)(C++20) |
检查谓词对于范围中的所有、任何或没有元素是否为 true (算法函数对象) |