Use a function adaptor in vector : Function Adaptor « Function « C++






Use a function adaptor in vector

Use a function adaptor in vector

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <cstring>
using namespace std;

int main()
{
  vector<char *> vectorObject;
  vector<char *>::iterator p;
  int i;

  vectorObject.push_back("One");
  vectorObject.push_back("Two");
  vectorObject.push_back("Three");
  vectorObject.push_back("Four");
  vectorObject.push_back("Five");

  cout << "Sequence contains:";
  for(i = 0; i <vectorObject.size(); i++)
    cout << vectorObject[ i ] << " ";
  cout << endl;

  cout << "Searching sequence for Three.\n";

  // use a pointer-to-function adaptor
  p = find_if(vectorObject.begin(), vectorObject.end(), not1(bind2nd(ptr_fun(strcmp), "Three")));

  if(p != vectorObject.end()) {
    cout << "Found.";
    cout << "Sequence from that point is:";
    do {
      cout << *p++ << " ";
    } while (p != vectorObject.end());
  }

  return 0;
}

           
       








Related examples in the same category

1.Use a unary function object to determine even/odd.Use a unary function object to determine even/odd.
2.Use member function reference function adaptorUse member function reference function adaptor