Namespaces
Variants

std::ranges:: all_of, std::ranges:: any_of, std::ranges:: 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
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
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutation operations
Fold operations
Operations on uninitialized storage
Return types
定义于头文件 <algorithm>
调用签名
template < std:: input_iterator I, std:: sentinel_for < I > S,

class Proj = std:: identity ,
std:: indirect_unary_predicate < std :: projected < I, Proj >> Pred >

constexpr bool all_of ( I first, S last, Pred pred, Proj proj = { } ) ;
(1) (C++20 起)
template < ranges:: input_range R, class Proj = std:: identity ,

std:: indirect_unary_predicate <
std :: projected < ranges:: iterator_t < R > ,Proj >> Pred >

constexpr bool all_of ( R && r, Pred pred, Proj proj = { } ) ;
(2) (C++20 起)
template < std:: input_iterator I, std:: sentinel_for < I > S,

class Proj = std:: identity ,
std:: indirect_unary_predicate < std :: projected < I, Proj >> Pred >

constexpr bool any_of ( I first, S last, Pred pred, Proj proj = { } ) ;
(3) (C++20 起)
template < ranges:: input_range R, class Proj = std:: identity ,

std:: indirect_unary_predicate <
std :: projected < ranges:: iterator_t < R > ,Proj >> Pred >

constexpr bool any_of ( R && r, Pred pred, Proj proj = { } ) ;
(4) (C++20 起)
template < std:: input_iterator I, std:: sentinel_for < I > S,

class Proj = std:: identity ,
std:: indirect_unary_predicate < std :: projected < I, Proj >> Pred >

constexpr bool none_of ( I first, S last, Pred pred, Proj proj = { } ) ;
(5) (C++20 起)
template < ranges:: input_range R, class Proj = std:: identity ,

std:: indirect_unary_predicate <
std :: projected < ranges:: iterator_t < R > ,Proj >> Pred >

constexpr bool none_of ( R && r, Pred pred, Proj proj = { } ) ;
(6) (C++20 起)
1) 检查一元谓词 pred 是否对范围 [ first , last ) 中的至少一个元素(通过投影函数 proj 映射后)返回 false
3) 检查一元谓词 pred 是否对范围 [ first , last ) 中的至少一个元素返回 true (在使用投影 proj 进行投影转换后)。
5) 检查一元谓词 pred 是否对范围 [ first , last ) 中的所有元素(通过投影 proj 处理后)均返回 true
2,4,6) (1,3,5) 相同,但使用 r 作为源范围,如同使用 ranges:: begin ( r ) 作为 first 以及 ranges:: end ( r ) 作为 last

本页面描述的函数式实体是 算法函数对象 (非正式称为 niebloids ),即:

目录

参数

first, last - 定义待检查元素范围的 范围 的迭代器-哨位对
r - 待检查元素的范围
pred - 应用于投影元素的谓词
proj - 应用于元素的投影

返回值

1,2) true 若对于范围内的每个迭代器 i std:: invoke ( pred, std:: invoke ( proj, * i ) ) ! = false 成立,否则返回 false 。若范围为空则返回 true
3,4) true 若对于范围内的至少一个迭代器 i 满足 std:: invoke ( pred, std:: invoke ( proj, * i ) ) ! = false ,否则返回 false 。若范围为空则返回 false
5,6) true 若对于范围内的每个迭代器 i std:: invoke ( pred, std:: invoke ( proj, * i ) ) == false 成立,否则返回 false 。若范围为空则返回 true
范围包含某些 true 元素
范围包含某些 false 元素 [1]
all_of false true false true
any_of true true false false
none_of false false true true
  1. 在此情况下该范围为空。

复杂度

最多 last - first 次谓词和投影函数的应用。

可能的实现

all_of (1,2)
struct all_of_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity,
             std::indirect_unary_predicate<std::projected<I, Proj>> Pred>
    constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const
    {
        return ranges::find_if_not(first, last, std::ref(pred), std::ref(proj)) == last;
    }
    template<ranges::input_range R, class Proj = std::identity,
             std::indirect_unary_predicate<
                 std::projected<ranges::iterator_t<R>,Proj>> Pred>
    constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const
    {
        return operator()(ranges::begin(r), ranges::end(r),
                          std::ref(pred), std::ref(proj));
    }
};
inline constexpr all_of_fn all_of;
any_of (3,4)
struct any_of_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity,
             std::indirect_unary_predicate<std::projected<I, Proj>> Pred>
    constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const
    {
        return ranges::find_if(first, last, std::ref(pred), std::ref(proj)) != last;
    }
    template<ranges::input_range R, class Proj = std::identity,
             std::indirect_unary_predicate<
                 std::projected<ranges::iterator_t<R>,Proj>> Pred>
    constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const
    {
        return operator()(ranges::begin(r), ranges::end(r),
                          std::ref(pred), std::ref(proj));
    }
};
inline constexpr any_of_fn any_of;
none_of (5,6)
struct none_of_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity,
             std::indirect_unary_predicate<std::projected<I, Proj>> Pred>
    constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const
    {
        return ranges::find_if(first, last, std::ref(pred), std::ref(proj)) == last;
    }
    template<ranges::input_range R, class Proj = std::identity,
             std::indirect_unary_predicate<
                 std

示例

#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
namespace ranges = std::ranges;
constexpr bool some_of(auto&& r, auto&& pred) // 部分而非全部
{
    return not (ranges::all_of(r, pred) or ranges::none_of(r, pred));
}
constexpr auto w = {1, 2, 3};
static_assert(!some_of(w, [](int x) { return x < 1; }));
static_assert( some_of(w, [](int x) { return x < 2; }));
static_assert(!some_of(w, [](int x) { return x < 4; }));
int main()
{
    std::vector<int> v(10, 2);
    std::partial_sum(v.cbegin(), v.cend(), v.begin());
    std::cout << "数列中的数字:";
    ranges::copy(v, std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
    if (ranges::all_of(v.cbegin(), v.cend(), [](int i) { return i % 2 == 0; }))
        std::cout << "所有数字均为偶数\n";
    if (ranges::none_of(v, std::bind(std::modulus<int>(), std::placeholders::_1, 2)))
        std::cout << "没有数字是奇数\n";
    auto DivisibleBy = [](int d)
    {
        return [d](int m) { return m % d == 0; };
    };
    if (ranges::any_of(v, DivisibleBy(7)))
        std::cout << "至少有一个数字能被7整除\n";
}

输出:

数列中的数字:2 4 6 8 10 12 14 16 18 20
所有数字均为偶数
没有数字是奇数
至少有一个数字能被7整除

参见

(C++11) (C++11) (C++11)
检查谓词是否对范围中的所有、任一或没有元素为 true
(函数模板)