Namespaces
Variants

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

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
all_of any_of none_of
(C++11) (C++11) (C++11)

Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
定义于头文件 <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 是否对范围 [ first , last ) 中的所有元素返回 true
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

(C++20 前)

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

(C++20 起)

目录

参数

first, last - 定义待检验元素范围的迭代器对
policy - 要使用的执行策略
p - 一元谓词

表达式 p ( v ) 必须可转换为 bool ,其中 v 是类型(可能为 const) VT 的参数, VT InputIt 的值类型,且与值类别无关,同时不得修改 v 。因此不允许参数类型为 VT & 也不允许为 VT ,除非对于 VT 移动操作等价于拷贝操作 (C++11 起) 。 ​

类型要求
-
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. 在此情况下该范围为空。

复杂度

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 << "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

参见

检查谓词是否对范围中的所有、任一或没有元素返回 true
(算法函数对象)