Use a pointer-to-function adapter, ptr_fun - C++ STL Algorithm

C++ examples for STL Algorithm:bind2nd

Description

Use a pointer-to-function adapter, ptr_fun

Demo Code

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <cstring>
using namespace std;
template<class InIter>
void show_range(const char *msg, InIter start, InIter end);
int main()/*from w  w  w .  j ava  2 s.c  o  m*/
{
   vector<char *> v;
   vector<char *>::iterator itr;
   v.push_back("One");
   v.push_back("Two");
   v.push_back("Three");
   v.push_back("Four");
   v.push_back("Five");

   show_range("Sequence contains: ", v.begin(), v.end());

   cout << endl;

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

   // Use a pointer-to-function adapter.

   itr = find_if(v.begin(), v.end(),

   not1(bind2nd(ptr_fun(strcmp), "Three")));

   if(itr != v.end()) {

      cout << "Found!\n";
      show_range("Sequence from that point is: ", itr, v.end());
   }
   return 0;
}
// Show a range of elements.
template<class InIter>
void show_range(const char *msg, InIter start, InIter end) {
   InIter itr;
   cout << msg;
   for(itr = start; itr != end; ++itr)
      cout << *itr << " ";
   cout << endl;
}

Result


Related Tutorials