Or login with:
#include <algorithm> template < class InputIterator, class Predicate> InputIterator find_if( InputIterator first, InputIterator last, Predicate pred );Parameters:
| Parameter | Description |
|---|---|
| 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 |
[first, last) for a value that satisfies the unary predicate pred. pred, or last if no such value is found.pred as the number of elements in the range [first,last).#include <iostream> #include <algorithm> #include <vector> using namespace std; bool isDivisibleByThree ( int n //in ) { return (n%3 == 0); } int main() { int a[] = {1, 2, 4, 5, 7, 12, 9, 8, 3, 6}; vector<int> v(a, a+10); cout <<"\nHere are the contents of v:\n"; for (vector<int>::size_type i=0; i<v.size(); i++) cout <<v.at(i)<<" "; vector<int>::iterator p; p = find_if(v.begin(), v.end(), isDivisibleByThree); if (p == v.end()) cout <<"\nThere are no values divisible by 3.\n"; else cout <<"\nThe first value in the sequence divisible by 3 is " <<*p<<",\nwhich was found at location " <<(int)(p-v.begin()+1)<<".\n"; return 0; }
You must login to leave a messge