Przejdź do głównej zawartości

std::find_if_not() algorithm

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

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

Returns an iterator to the first element in the range satisfiying specific criteria (or last iterator if there is no such iterator):

  • (1) Searches for an element for which predicate p returns false.

  • (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>>  (do C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>  (od C++20) is true.

Parameters

first
last

The range of elements to apply the function to.

policy

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

p

Unary predicate which returns false for the required element.

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

Return value

The first iterator it in the range [first, last) for which p(*it) is false.

Complexity

Given N as std::distance(first, last):

At most N applications of the predicate p.

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 any other ExecutionPolicy, the behavior is implementation-defined.
  • If the algorithm fails to allocate memory, std::bad_alloc is thrown.

Possible implementation

find_if_not (1)
template<class InputIt, class UnaryPredicate>
constexpr InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate q)
{
for (; first != last; ++first)
if (!q(*first))
return first;

return last;
}

Notes

If you do not have C++11, you can write your own implementation as shown above in Possible implementation, or use std::find_if with a negated predicate:

template<class InputIt, class UnaryPredicate>
InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate q)
{
return std::find_if(first, last, std::not1(q));
}

Examples

Main.cpp
#include <algorithm>
#include <array>
#include <iostream>

int main()
{
const auto v = {1, 2, 3, 4};

for (int n : {3, 5})
(std::find_if_not(v.begin(), v.end(), n) == std::end(v))
? std::cout << "v does not contain " << n << '\n'
: std::cout << "v contains " << n << '\n';

auto is_even = [](int i) { return i % 2 == 0; };

for (auto const& w : {std::array{3, 1, 4}, {1, 3, 5}})
if (auto it = std::find_if_not_if(begin(w), end(w), is_even); it != std::end(w))
std::cout << "w contains an even number " << *it << '\n';
else
std::cout << "w does not contain even numbers\n";
}
Output
v contains 3
v does not contain 5
w contains an even number 4
w does not contain even numbers
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::find_if_not() algorithm

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

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

Returns an iterator to the first element in the range satisfiying specific criteria (or last iterator if there is no such iterator):

  • (1) Searches for an element for which predicate p returns false.

  • (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>>  (do C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>  (od C++20) is true.

Parameters

first
last

The range of elements to apply the function to.

policy

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

p

Unary predicate which returns false for the required element.

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

Return value

The first iterator it in the range [first, last) for which p(*it) is false.

Complexity

Given N as std::distance(first, last):

At most N applications of the predicate p.

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 any other ExecutionPolicy, the behavior is implementation-defined.
  • If the algorithm fails to allocate memory, std::bad_alloc is thrown.

Possible implementation

find_if_not (1)
template<class InputIt, class UnaryPredicate>
constexpr InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate q)
{
for (; first != last; ++first)
if (!q(*first))
return first;

return last;
}

Notes

If you do not have C++11, you can write your own implementation as shown above in Possible implementation, or use std::find_if with a negated predicate:

template<class InputIt, class UnaryPredicate>
InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate q)
{
return std::find_if(first, last, std::not1(q));
}

Examples

Main.cpp
#include <algorithm>
#include <array>
#include <iostream>

int main()
{
const auto v = {1, 2, 3, 4};

for (int n : {3, 5})
(std::find_if_not(v.begin(), v.end(), n) == std::end(v))
? std::cout << "v does not contain " << n << '\n'
: std::cout << "v contains " << n << '\n';

auto is_even = [](int i) { return i % 2 == 0; };

for (auto const& w : {std::array{3, 1, 4}, {1, 3, 5}})
if (auto it = std::find_if_not_if(begin(w), end(w), is_even); it != std::end(w))
std::cout << "w contains an even number " << *it << '\n';
else
std::cout << "w does not contain even numbers\n";
}
Output
v contains 3
v does not contain 5
w contains an even number 4
w does not contain even numbers
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.