Man Page find_if.3

                       Standard C++ Library
             Copyright 1998, Rogue Wave Software, Inc.



      - Finds an occurrence of a value in a sequence that  satis-
     fies a specified predicate.


     #include <algorithm>
     template <class InputIterator, class Predicate>
      InputIterator find_if(InputIterator first,
                            InputIterator last,
                            Predicate pred);


     The find_if algorithm allows you to  search  for  the  first
     element in a sequence that satisfies a particular condition.
     The sequence is defined by iterators first and  last,  while
     the  condition is defined by the third argument: a predicate
     function that returns a boolean value. find_if  returns  the
     first  iterator  i  in the range [first, last) for which the
     following condition holds:

     pred(*i) == true.

     If no such iterator is found, find_if returns last.


     find_if performs at  most  last-first  applications  of  the
     corresponding predicate.


     // find.cpp
     #include <vector>
     #include <algorithm>
     #include <functional>
     #include <iostream>
     using namespace std;

     int main()
       typedef vector<int>::iterator iterator;
       int d1[10] = {0,1,2,2,3,4,2,2,6,7};

        // Set up a vector
       vector<int> v1(d1,d1 + 10);

        // Try find
       iterator it1 = find(v1.begin(),v1.end(),3);
        // it1 = v1.begin() + 4;

        // Try find_if
       iterator it2 =
        // it2 = v1.begin() + 4

        // Try both adjacent_find variants
       iterator it3 = adjacent_find(v1.begin(),v1.end());
        // it3 = v1.begin() +2

       iterator it4 =
        // v4 = v1.begin() + 2

        // Output results
       cout << *it1 << " " << *it2 << " " << *it3 << " "
             << *it4 << endl;

       return 0;

     Program Output

     3 3 2 2


     If your compiler does not support default  template  parame-
     ters,  then you always need to supply the Allocator template
     argument. For instance, you have to write:

     vector<int, allocator<int> >
     instead of:


     If your compiler does not support namespaces,  then  you  do
     not need the using declaration for std.


     adjacent_find, Algorithms, find, find_end, find_first_of