Przejdź do głównej zawartości

std::ranges::none_of() algorithm

// (1)
constexpr bool none_of( I first, S last, Pred pred, Proj proj = {} );

// (2)
constexpr bool none_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>>

The Proj template argument has a default type of std::identity for all overloads.

Checks if a predicate holds for none of the elements in a range.

  • (1) Checks if unary predicate pred returns true for none of the elements in the range [first; last) (after projecting with the projection proj).

  • (2) Same as (1), 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.

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.

important

Returns true if the range is empty.

Complexity

At most last - first applications of predicate and projection.

Exceptions

(none)

Possible implementation

none_of(1) and all_of(2)
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;

Examples

Main.cpp
#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::none_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::none_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
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::ranges::none_of() algorithm

// (1)
constexpr bool none_of( I first, S last, Pred pred, Proj proj = {} );

// (2)
constexpr bool none_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>>

The Proj template argument has a default type of std::identity for all overloads.

Checks if a predicate holds for none of the elements in a range.

  • (1) Checks if unary predicate pred returns true for none of the elements in the range [first; last) (after projecting with the projection proj).

  • (2) Same as (1), 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.

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.

important

Returns true if the range is empty.

Complexity

At most last - first applications of predicate and projection.

Exceptions

(none)

Possible implementation

none_of(1) and all_of(2)
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;

Examples

Main.cpp
#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::none_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::none_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
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.