std::ranges...es::none_of(3)
π std::ranges...es::none_of(3) β C++ Standard Libary π
πΈ NAME πΈ
std::ranges::all_of,std::ranges::any_of,std::ranges::none_of β std::ranges::all_of, std::ranges::any_of, std::ranges::none_of. That's the trio, no cap. π
β¨ Synopsis β¨
Defined in header <algorithm> β yes, that exact header. Call signature time:
template< std::input_iterator I, std::sentinel_for<I> S,
class Proj = std::identity,
std::indirect_unary_predicate<std::projected<I, Proj>>
(1) (since C++20)
Pred >
constexpr bool
all_of( I first, S last, Pred pred, Proj proj = {} );
template< ranges::input_range R, class Proj = std::identity,
std::indirect_unary_predicate<
std::projected<ranges::iterator_t<R>,Proj>> Pred >
(2) (since C++20)
constexpr bool
all_of( R&& r, Pred pred, Proj proj = {} );
template< std::input_iterator I, std::sentinel_for<I> S,
class Proj = std::identity,
std::indirect_unary_predicate<std::projected<I, Proj>>
(3) (since C++20)
Pred >
constexpr bool
any_of( I first, S last, Pred pred, Proj proj = {} );
template< ranges::input_range R, class Proj = std::identity,
std::indirect_unary_predicate<
std::projected<ranges::iterator_t<R>,Proj>> Pred >
(4) (since C++20)
constexpr bool
any_of( R&& r, Pred pred, Proj proj = {} );
template< std::input_iterator I, std::sentinel_for<I> S,
class Proj = std::identity,
std::indirect_unary_predicate<std::projected<I, Proj>>
(5) (since C++20)
Pred >
constexpr bool
none_of( I first, S last, Pred pred, Proj proj = {} );
template< ranges::input_range R, class Proj = std::identity,
std::indirect_unary_predicate<
std::projected<ranges::iterator_t<R>,Proj>> Pred >
(6) (since C++20)
constexpr bool
none_of( R&& r, Pred pred, Proj proj = {} );
(1)checks if unary predicatepredreturnstruefor all elements in the range[first, last)after projecting with the projectionproj.(3)checks if unary predicatepredreturnstruefor at least one element in the range[first, last)after projecting with the projectionproj.(5)checks if unary predicatepredreturnstruefor no elements in the range[first, last)after projecting with the projectionproj.(2,4,6)same as(1,3,5), but usesras the source range, as if usingranges::begin(r)asfirstandranges::end(r)aslast.
The function-like entities described on this page are niebloids, that is:
- Explicit template argument lists cannot be specified when calling any of them.
- None of them are visible to argument-dependent lookup.
- When any of them are found by normal unqualified lookup as the name to the left of the function-call operator, argument-dependent lookup is inhibited.
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)trueifstd::invoke(pred, std::invoke(proj, *i)) != falsefor every iteratoriin the range,falseotherwise. Returnstrueif the range is empty.(3,4)trueifstd::invoke(pred, std::invoke(proj, *i)) != falsefor at least one iteratoriin the range,falseotherwise. Returnsfalseif the range is empty.(5,6)trueifstd::invoke(pred, std::invoke(proj, *i)) == falsefor every iteratoriin the range,falseotherwise. Returnstrueif the range is empty.
Also: see Notes below. π
π₯ Complexity π₯
At most last - first applications of the predicate and the projection.
π§ Possible implementation π§
π 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;
π¦ Notes π¦
The return value represented in the form of the Truth table is:
- input range contains all true, none false β
(1,2)all_of:true;(3,4)any_of:true;(5,6)none_of:false - input range contains some true, some false β
(1,2)all_of:false;(3,4)any_of:true;(5,6)none_of:false - input range contains none true, all false β
(1,2)all_of:false;(3,4)any_of:false;(5,6)none_of:true - input range contains none true, none false (empty range) β
(1,2)all_of:true;(3,4)any_of:false;(5,6)none_of:true
π Example π
Run this code:
// Run this code
#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";
}
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 π
all_ofβ checks if a predicate is true for all, any or none of the elements in a range.(C++11)(function template)any_ofβ checks if a predicate is true for all, any or none of the elements in a range.(C++11)none_ofβ checks if a predicate is true for all, any or none of the elements in a range.(C++11)
π«Ά that's the whole page, bestie. source:http://cppreference.comΒ· date/version:2024.06.10Β· page:std::ranges...es::none_of(3)β mwah π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms