find_if
Locates the position of the first occurrence of an element in a range that satisfies a specified condition.
template<class InputIterator, class Predicate>
InputIterator find_if(
InputIterator _First,
InputIterator _Last,
Predicate _Pred
);
Parameters
_First
An input iterator addressing the position of the first element in the range to be searched._Last
An input iterator addressing the position one past the final element in the range to be searched._Pred
User-defined predicate function object that defines the condition to be satisfied by the element being searched for. A predicate takes single argument and returns true or false.
Return Value
An input iterator that addresses the first element in the range that satisfies the condition specified by the predicate.
Remarks
This template function is a generalization of the algorithm find, replacing the predicate "equals a specific value" with any predicate.
Example
// alg_find_if.cpp
// compile with: /EHsc
#include <list>
#include <algorithm>
#include <iostream>
bool greater10 ( int value )
{
return value >10;
}
int main( )
{
using namespace std;
list <int> L;
list <int>::iterator Iter;
list <int>::iterator result;
L.push_back( 5 );
L.push_back( 10 );
L.push_back( 15 );
L.push_back( 20 );
L.push_back( 10 );
cout << "L = ( " ;
for ( Iter = L.begin( ) ; Iter != L.end( ) ; Iter++ )
cout << *Iter << " ";
cout << ")" << endl;
result = find_if( L.begin( ), L.end( ), &greater10 );
if ( result == L.end( ) )
cout << "There is no element greater than 10 in list L."
<< endl;
else
{
result++;
cout << "There is an element greater than 10 in list L,"
<< "\n and it is followed by a "
<< *(result) << "." << endl;
}
}
L = ( 5 10 15 20 10 ) There is an element greater than 10 in list L, and it is followed by a 20.
Requirements
Header: <algorithm>
Namespace: std