命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | algorithm‎ | ranges
 
 
算法库
约束算法和范围上的算法 (C++20)
约束算法,例如 ranges::copy, ranges::sort, ...
执行策略 (C++17)
排序和相关操作
分区操作
排序操作
二分搜索操作
(在已分区范围上)
集合操作 (在已排序范围上)
合并操作 (在已排序范围上)
堆操作
最小值/最大值操作
(C++11)
(C++17)
字典序比较操作
排列操作
C 库
数值操作
未初始化内存上的操作
 
约束算法
此菜单中的所有名称都属于命名空间 std::ranges
非修改序列操作
修改序列操作
分区操作
排序操作
二分搜索操作 (在已排序范围上)
       
       
集合操作 (在已排序范围上)
堆操作
最小值/最大值操作
       
       
排列操作
折叠操作
数值操作
(C++23)            
未初始化存储上的操作
返回类型
 
定义于头文件 <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) (since 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) (since 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) (since 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) (since 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) (since 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) (since C++20)
1) 检查一元谓词 pred 对于范围 [firstlast) 中的至少一个元素(在使用投影 proj 投影后)是否返回 false
3) 检查一元谓词 pred 对于范围 [firstlast) 中的至少一个元素(在使用投影 proj 投影后)是否返回 true
5) 检查一元谓词 pred 对于范围 [firstlast) 中的所有元素(在使用投影 proj 投影后)是否都不返回 true
2,4,6)(1,3,5) 相同,但使用 r 作为源范围,就像使用 ranges::begin(r) 作为 firstranges::end(r) 作为 last

此页面上描述的类似函数的实体是算法函数对象(非正式地称为 niebloids),即

目录

[编辑] 参数

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

[编辑] 返回值

1,2) 如果对于范围中的每个迭代器 istd::invoke(pred, std::invoke(proj, *i)) != false,则返回 true,否则返回 false。如果范围为空,则返回 true
3,4) 如果对于范围中的至少一个迭代器 istd::invoke(pred, std::invoke(proj, *i)) != false,则返回 true,否则返回 false。如果范围为空,则返回 false
5,6) 如果对于范围中的每个迭代器 istd::invoke(pred, std::invoke(proj, *i)) == false,则返回 true,否则返回 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::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 none_of_fn none_of;

[编辑] 示例

#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
 
namespace ranges = std::ranges;
 
constexpr bool some_of(auto&& r, auto&& pred) // some but not all
{
    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 << "Among the numbers: ";
    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 << "All numbers are even\n";
 
    if (ranges::none_of(v, std::bind(std::modulus<int>(), std::placeholders::_1, 2)))
        std::cout << "None of them are odd\n";
 
    auto DivisibleBy = [](int d)
    {
        return [d](int m) { return m % d == 0; };
    };
 
    if (ranges::any_of(v, 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++11)(C++11)(C++11)
检查谓词对于范围中的所有、任何或没有元素是否为 true
(函数模板) [编辑]