std::ranges::all_of() algorithm
- od C++20
- Simplified
- Detailed
// (1)
constexpr bool all_of( I first, S last, Pred pred, Proj proj = {} );
// (2)
constexpr bool all_of( R&& r, Pred pred, Proj proj = {} );
The type of arguments are generic and have the following constraints:
I
-std::input_iterator
S
-std::sentinel_for<I>
R
-ranges::input_range
Proj
- (none)Pred
:- (1) -
std::indirect_unary_predicate<std::projected<I, Proj>>
- (2) -
std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>>
- (1) -
The Proj
template argument has a default type of std::identity
for all overloads.
// (1)
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 = {} );
// (2)
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 = {} );
Checks if a predicate holds for all of the elements in a range.
-
(1) Checks if unary predicate
pred
returnstrue
for all elements in the range [first
;last
) (after projecting with the projectionproj
). -
(2) Same as (1), but uses
r
as the source range, as if usingranges::begin(r)
asfirst
andranges::end(r)
aslast
.
The function-like entities described on this page are niebloids.
Parameters
first last | The range of elements to examine. |
r | The range of elements to examine. |
proj | Projection to apply to the elements. |
pred | Predicate to apply to projected elements. |
Return value
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 predicate and projection.
Exceptions
(none)
Possible implementation
all_of(1) and all_of(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;
Examples
#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
Hover to see the original license.