std::all_of, std::any_of, std::none_of

来自cppreference.com
< cpp‎ | algorithm
 
 
算法库
受约束算法及范围上的算法 (C++20)
包含算法例如 ranges::copy, ranges::sort, ...
执行策略 (C++17)
排序和相关操作
划分操作
排序操作
二分搜索操作(在已划分范围上)
集合操作(在有序范围上)
归并操作(在有序范围上)
堆操作
最小/最大操作
(C++11)
(C++17)
字典序比较操作
排列操作
C 库

数值运算
(C++11)                       
在未初始化内存上的操作
 
在标头 <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,

             ForwardIt first, ForwardIt last, UnaryPred p );
(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,

             ForwardIt first, ForwardIt last, UnaryPred p );
(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,

              ForwardIt first, ForwardIt last, UnaryPred p );
(6) (C++17 起)
1) 检查一元谓词 p 是否对范围 [firstlast) 中所有元素返回 true
3) 检查一元谓词 p 是否对范围 [firstlast) 中至少一个元素返回 true
5) 检查一元谓词 p 是否不对范围 [firstlast) 中任何元素返回 true
2,4,6)(1,3,5),但按照 policy 执行。这些重载只有在

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>

(C++20 前)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>

(C++20 起)
true 时时才会参与重载决议。

参数

first, last - 要检验的元素范围
policy - 所用的执行策略。细节见执行策略
p - 一元谓词。

对每个(可为 const 的) VT 类型参数 v ,其中 VTInputIt 的值类型,表达式 p(v) 必须可转换到 bool,无关乎值类别,而且必须不修改 v 。从而不允许 VT& 类型参数,亦不允许 VT ,除非对 VT 而言移动等价于复制 (C++11 起)。 ​

类型要求
-
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

复杂度

1-6) 应用最多 std::distance(first, last) 次谓词 p

异常

拥有名为 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 << "在这些数字中:";
    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 整除

参阅

检查谓词是否对范围中所有、任一或无元素为 true
(niebloid)