◐ Shell
clean mode source ↗

std::all_of, std::any_of, std::none_of - cppreference.com

来自cppreference.com

在标头 <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 是否对范围 [firstlast) 中所有元素返回 true

2) 检查一元谓词 p 是否对范围 [firstlast) 中至少一个元素返回 true

3) 检查一元谓词 p 是否不对范围 [firstlast) 中任何元素返回 true

4-6)(1-3),但按照 policy 执行。

这些重载只有在以下表达式的值是 true 时才会参与重载决议:

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

(C++20 前)

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

(C++20 起)

参数

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 元素      否[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

异常

注解

功能特性测试 标准 功能特性
__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 << "在这些数字中:";
    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 整除

参阅