find_if c++ example
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> myvec {2, 5, 6, 10, 56, 7, 48, 89};
vector<int>::iterator gt10 = find_if(myvec.begin(), myvec.end(), [](int x){return x>10;}); // >= C++11
cout << "First item > 10: " << *gt10 << endl;
//check if pointer points to myvec.end()
if(gt10 != myvec.end()) {
cout << "nf points to: " << *gt10 << endl;
}
else {
cout << "item not found" << endl;
}
return 0;
}
InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);
first, last :range which contains all the elements between first
and last, including the element pointed by first but
not the element pointed by last.
pred : Unary function that accepts an element in the range
as argument and returns a value in boolean.
Return value :
Returns an iterator to the first element in the range
[first, last] for which pred(function) returns true. If
no such element is found, the function returns last.