home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 May / VPR9705A.ISO / VPR_DATA / PROGRAM / CBTRIAL / SETUP / DATA.Z / BINDERS.CPP < prev    next >
Text File  |  1997-02-14  |  1KB  |  40 lines

  1.  #include <functional>
  2.  #include <algorithm>
  3.  #include <vector>
  4.  
  5.  using namespace std;
  6.  
  7.  int main ()
  8.  {
  9.    typedef vector<int>::iterator iterator;
  10.    int d1[4] = {1,2,3,4};
  11.    //
  12.    // Set up a vector.
  13.    //
  14.    vector<int> v1(d1+0, d1+4);
  15.    //
  16.    // Create an 'equal to 3' unary predicate by binding 3 to
  17.    // the equal_to binary predicate.
  18.    //
  19.    binder1st<equal_to<int> > equal_to_3 = bind1st(equal_to<int>(),3);
  20.    //
  21.    // Now use this new predicate in a call to find_if.
  22.    //
  23.    iterator it1 = find_if(v1.begin(),v1.end(),equal_to_3);
  24.    //
  25.    // Even better, construct the new predicate on the fly.
  26.    //
  27.    iterator it2 = find_if(v1.begin(),v1.end(),bind1st(equal_to<int>(),3));
  28.    //
  29.    // And now the same thing using bind2nd.
  30.    // Same result since == is commutative.
  31.    //
  32.    iterator it3 = find_if(v1.begin(),v1.end(),bind2nd(equal_to<int>(),3));
  33.    //
  34.    // Output results.
  35.    //
  36.    cout << *it1 << " " << *it2 << " " << *it3 << endl;
  37.  
  38.    return 0;
  39.  }
  40.