Przejdź do głównej zawartości

std::none_of() algorithm

// (1)
template< class InputIt, class UnaryPredicate >
constexpr bool none_of( InputIt first, InputIt last, UnaryPredicate p );

// (2)
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate >
bool none_of( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last,
UnaryPredicate p );

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

  • (1) Checks if unary predicate p returns false for every element in the range [first; last).

  • (2) Same as (1), but executed according to policy.

    Overload Resolution

    These overloads participate in overload resolution only if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true.  (do C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> is true.  (od C++20)

Parameters

first
second

The range of elements to examine.

policy

The execution policy to use. See execution policy for details.

p

Unary predicate.

The expression p(v) must be convertible to bool for every argument v of type (possibly const) VT, where VT is the value type of InputIt, regardless of value category, and must not modify v. Thus, a parameter type of VT& is not allowed , nor is VT unless for VT a move is equivalent to a copy. (od C++11).

Type requirements

InputItLegacyInputIterator
ForwardItLegacyForwardIterator
UnaryPredicatePredicate

Return value

true if unary predicate returns true for no elements in the range, false otherwise.

important

Returns true if the range is empty.

Complexity

  • (1) At most last - first applications of the predicate.
  • (2) O(last - first) applications of the predicate.

Exceptions

The overloads with a template parameter named ExecutionPolicy report errors as follows:

  • If execution of a function invoked as part of the algorithm throws an exception and ExecutionPolicy is one of the standard policies, std::terminate is called. For none other ExecutionPolicy, the behavior is implementation-defined.
  • If the algorithm fails to allocate memory, std::bad_alloc is thrown.

Possible implementation

none_of (1)
template<class InputIt, class UnaryPredicate>
constexpr bool none_of(InputIt first, InputIt last, UnaryPredicate p)
{
return std::find_if(first, last, p) == last;
}

Examples

Main.cpp
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>

int main()
{
std::vector<int> v(10, 2);
std::partial_sum(v.cbegin(), v.cend(), v.begin());
std::cout << "Among the numbers: ";
std::copy(v.cbegin(), v.cend(), std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';

if (std::none_of(v.cbegin(), v.cend(), [](int i) { return i % 2 == 0; }))
std::cout << "All numbers are even\n";

if (std::none_of(v.cbegin(), v.cend(), std::bind(std::modulus<>(),
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 (std::none_of(v.cbegin(), v.cend(), 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::none_of() algorithm

// (1)
template< class InputIt, class UnaryPredicate >
constexpr bool none_of( InputIt first, InputIt last, UnaryPredicate p );

// (2)
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate >
bool none_of( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last,
UnaryPredicate p );

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

  • (1) Checks if unary predicate p returns false for every element in the range [first; last).

  • (2) Same as (1), but executed according to policy.

    Overload Resolution

    These overloads participate in overload resolution only if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true.  (do C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> is true.  (od C++20)

Parameters

first
second

The range of elements to examine.

policy

The execution policy to use. See execution policy for details.

p

Unary predicate.

The expression p(v) must be convertible to bool for every argument v of type (possibly const) VT, where VT is the value type of InputIt, regardless of value category, and must not modify v. Thus, a parameter type of VT& is not allowed , nor is VT unless for VT a move is equivalent to a copy. (od C++11).

Type requirements

InputItLegacyInputIterator
ForwardItLegacyForwardIterator
UnaryPredicatePredicate

Return value

true if unary predicate returns true for no elements in the range, false otherwise.

important

Returns true if the range is empty.

Complexity

  • (1) At most last - first applications of the predicate.
  • (2) O(last - first) applications of the predicate.

Exceptions

The overloads with a template parameter named ExecutionPolicy report errors as follows:

  • If execution of a function invoked as part of the algorithm throws an exception and ExecutionPolicy is one of the standard policies, std::terminate is called. For none other ExecutionPolicy, the behavior is implementation-defined.
  • If the algorithm fails to allocate memory, std::bad_alloc is thrown.

Possible implementation

none_of (1)
template<class InputIt, class UnaryPredicate>
constexpr bool none_of(InputIt first, InputIt last, UnaryPredicate p)
{
return std::find_if(first, last, p) == last;
}

Examples

Main.cpp
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>

int main()
{
std::vector<int> v(10, 2);
std::partial_sum(v.cbegin(), v.cend(), v.begin());
std::cout << "Among the numbers: ";
std::copy(v.cbegin(), v.cend(), std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';

if (std::none_of(v.cbegin(), v.cend(), [](int i) { return i % 2 == 0; }))
std::cout << "All numbers are even\n";

if (std::none_of(v.cbegin(), v.cend(), std::bind(std::modulus<>(),
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 (std::none_of(v.cbegin(), v.cend(), 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.