std::all_of, std::any_of, std::none_of
来自 zh.cppreference.net
| 定义于头文件 <algorithm>
|
||
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 是否对范围 true 中的所有元素返回 [first, last)。2) 检查一元谓词
p 是否对范围 true 中的至少一个元素返回 [first, last)。3) 检查一元谓词
p 是否对范围 true 中没有元素返回 [first, last)。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。异常
4-6) 在执行过程中:
- 如果并行化所需的临时内存资源不可用,则抛出 std::bad_alloc。
- 如果在通过算法参数访问对象时抛出未捕获的异常,则行为由执行策略决定(对于标准策略,会调用 std::terminate)。
注解
| 特性测试宏 | 值 | 标准 | 特性 |
|---|---|---|---|
__cpp_lib_parallel_algorithm |
202506L |
(C++26) | 并行范围算法,(4-6) |
可能的实现
另见以下实现:
| 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";
using namespace std::placeholders;
if (std::none_of(v.cbegin(), v.cend(), std::bind(std::modulus<>(), _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(算法函数对象) |