C ++ STL's Non-mutating algorithms is a set of template functions that do not destroy operation data, it is used for processing sequence data one by one, element search, subsequence search, statistics, and matching.
The find_if algorithm is a predicate version of the find algorithm. It uses the predicates that return boolean values to determine the pred and checks every element in the iterator range [first, last, if the iterator iter satisfies pred (* iter) = true, the element is found and the iterator value iter is returned. If no element is found, the last is returned.
Function prototype:
Template <class InputIterator, class Predicate>
InputIterator find_if (
InputIterator _ First,
InputIterator _ Last,
Predicate _ Pred
);
Sample Code:
/*************************************** ****************************
* Copyright (C) Jerry Jiang
* File Name: find_if.cpp
* Author: Jerry Jiang
* Create Time: 2011-9-29 22:21:29
* Mail: jbiaojerry@gmail.com
* Blog: http://blog.csdn.net/jerryjbiao
* Description: The third in the C ++ STL algorithm series interpreted by a simple program.
* Non-variable algorithm: Conditional search for the container element find_if
**************************************** **************************/
# Include <algorithm>
# Include <vector>
# Include <iostream>
Using namespace std;
// The predicate judgment function divbyfive: determines whether x can divide x by 5.
Bool divbyfive (int x)
{
Return x % 5? 0: 1;
}
Int main ()
{
// Initial vector
Vector <int> iVect (20 );
For (size_t I = 0; I <iVect. size (); ++ I)
{
IVect [I] = (I + 1) * (I + 3 );
}
Vector <int>: iterator iLocation;
ILocation = find_if (iVect. begin (), iVect. end (), divbyfive );
If (iLocation! = IVect. end ())
{
Cout <"the first element that can be divisible by 5 is :"
<* ILocation <endl // print element: 15
<"The index position of an element is :"
<ILocation-iVect. begin () <endl; // print the index location: 2
}
Return 0;
}
From: Jerry Jiang's program life