find_if
Syntax:
#include <algorithm> iterator find_if( iterator start, iterator end, UnPred up ); The find_if() function searches for the first element between start and end for which the unary predicate up returns true. If such an element is found, an iterator pointing to that element is returned. Otherwise, an iterator pointing to end is returned. For example, the following code uses find_if() and a "greater-than-zero" unary predicate to the first positive, non-zero number in a list of numbers: int nums[] = { 0, -1, -2, -3, -4, 342, -5 }; int* result; int start = 0; int end = 7; result = find_if( nums + start, nums + end, bind2nd(greater<int>(), 0)); if( *result == nums[end] ) { cout << "Did not find any number greater than zero" << endl; } else { cout << "Found a positive non-zero number: " << *result << endl; } |