std::find_if() algorithm
- since C++20
- since C++17
- until C++17
// (1)
template< class InputIt, class UnaryPredicate >
constexpr InputIt find_if( InputIt first, InputIt last, UnaryPredicate p );
// (2)
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate >
ForwardIt find_if( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, UnaryPredicate p );
// (1)
template< class InputIt, class UnaryPredicate >
InputIt find_if( InputIt first, InputIt last, UnaryPredicate p );
// (2)
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate >
ForwardIt find_if( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, UnaryPredicate p );
// (1)
template< class InputIt, class UnaryPredicate >
InputIt find_if( InputIt first, InputIt 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
returnstrue
. -
(2) Same as (1), but executed according to
policy
.Overload ResolutionThese overloads participate in overload resolution only if
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>
(until C++20)std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>
(since C++20) istrue
.
Parameters
first second | 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 The expression |
Type requirements
InputIt | LegacyInputIterator |
ForwardIt | LegacyForwardIterator |
Return value
The first iterator it in the range [first
, last
) for which p(*it)
is true
.
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 otherExecutionPolicy
, the behavior is implementation-defined. - If the algorithm fails to allocate memory,
std::bad_alloc
is thrown.
Possible implementation
find_if (1)
template<class InputIt, class UnaryPredicate>
constexpr InputIt find_if(InputIt first, InputIt last, UnaryPredicate p)
{
for (; first != last; ++first)
if (p(*first))
return first;
return last;
}
Examples
#include <algorithm>
#include <array>
#include <iostream>
int main()
{
const auto v = {1, 2, 3, 4};
for (int n : {3, 5})
(std::find_if(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_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";
}
v contains 3
v does not contain 5
w contains an even number 4
w does not contain even numbers
Hover to see the original license.