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

From cppreference.com
< cpp‎ | algorithm‎ | ranges
 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Concepts and utilities: std::Sortable, std::projected, ...
Constrained algorithms: std::ranges::copy, std::ranges::sort, ...
Execution policies (C++17)
Non-modifying sequence operations
(C++11)(C++11)(C++11)
(C++17)
Modifying sequence operations
Operations on uninitialized storage
Partitioning operations
Sorting operations
(C++11)
Binary search operations
Set operations (on sorted ranges)
Heap operations
(C++11)
Minimum/maximum operations
(C++11)
(C++17)
Permutations
Numeric operations
C library
 
Constrained algorithms
Non-modifying sequence operations
all_ofany_ofnone_of
                                        
                                        
Modifying sequence operations
Operations on uninitialized storage
Partitioning operations
                                                  
Sorting operations
Binary search operations
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutations
 
Defined in header <algorithm>
Call signature
template< InputIterator I, Sentinel<I> S, class Proj = std::identity,

          IndirectUnaryPredicate<projected<I, Proj>> Pred >

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

          IndirectUnaryPredicate<projected<ranges::iterator_t<R>, Proj>> Pred >

bool all_of( R&& r, Pred pred, Proj proj = {} );
(2) (since C++20)
template< InputIterator I, Sentinel<I> S, class Proj = std::identity,

          IndirectUnaryPredicate<projected<I, Proj>> Pred >

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

          IndirectUnaryPredicate<projected<ranges::iterator_t<R>, Proj>> Pred >

bool any_of( R&& r, Pred pred, Proj proj = {} );
(4) (since C++20)
template< InputIterator I, Sentinel<I> S, class Proj = std::identity,

          IndirectUnaryPredicate<projected<I, Proj>> Pred >

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

          IndirectUnaryPredicate<projected<ranges::iterator_t<R>, Proj>> Pred >

bool none_of( R&& r, Pred pred, Proj proj = {} );
(6) (since C++20)
1) Checks if unary predicate pred returns true for all elements in the range [first, last) (after projecting with the projection proj).
3) Checks if unary predicate pred returns true for at least one element in the range [first, last) (after projecting with the projection proj).
5) Checks if unary predicate pred returns true for no elements in the range [first, last) (after projecting with the projection proj).
2,4,6) Same as (1,3,5), but uses r as the source range, as if using ranges::begin(r) as first and ranges::end(r) as last.

The function-like entities described on this page are niebloids, that is:

In practice, they may be implemented as function objects, or with special compiler extensions.

Parameters

first, last - the range of the elements to examine
r - the range of the elements to examine
pred - predicate to apply to the projected elements
proj - projection to apply to the elements

Return value

1-2) true if std::invoke(pred, std::invoke(proj, *i)) != false for every iterator i in the range, false otherwise. Returns true if the range is empty.
3-4) true if std::invoke(pred, std::invoke(proj, *i)) != false for at least one iterator i in the range, false otherwise. Returns false if the range is empty.
5-6) true if std::invoke(pred, std::invoke(proj, *i)) == false for every iterator i in the range, false otherwise. Returns true if the range is empty.

Complexity

At most last - first applications of the predicate and the projection.

Possible implementation

First version
struct all_of_fn {
  template< InputIterator I, Sentinel<I> S, class Proj = std::identity,
            IndirectUnaryPredicate<projected<I, Proj>> Pred >
  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< InputRange R, class Proj = std::identity,
            IndirectUnaryPredicate<projected<ranges::iterator_t<R>, Proj>> Pred >
  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;
Second version
struct any_of_fn {
  template< InputIterator I, Sentinel<I> S, class Proj = std::identity,
            IndirectUnaryPredicate<projected<I, Proj>> Pred >
  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< InputRange R, class Proj = std::identity,
            IndirectUnaryPredicate<projected<ranges::iterator_t<R>, Proj>> Pred >
  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;
Third version
struct none_of_fn {
  template< InputIterator I, Sentinel<I> S, class Proj = std::identity,
            IndirectUnaryPredicate<projected<I, Proj>> Pred >
  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< InputRange R, class Proj = std::identity,
            IndirectUnaryPredicate<projected<ranges::iterator_t<R>, Proj>> Pred >
  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;

Example

#include <vector>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <iterator>
#include <iostream>
#include <functional>
 
namespace ranges = std::ranges;
 
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";
    }
    struct DivisibleBy
    {
        const int d;
        DivisibleBy(int n) : d(n) {}
        bool operator()(int n) const { return n % d == 0; }
    };
 
    if (ranges::any_of(v, DivisibleBy(7))) {
        std::cout << "At least one number is divisible by 7\n";
    }
}

Output:

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

See also

(C++11)(C++11)(C++11)
checks if a predicate is true for all, any or none of the elements in a range
(function template)