Using not1() to sort a sequence with function object and use not2() to remove all elements not equal to H. - C++ STL Algorithm

C++ examples for STL Algorithm:not2

Description

Using not1() to sort a sequence with function object and use not2() to remove all elements not equal to H.

Demo Code

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
template<class InIter>
void show_range(const char *msg, InIter start, InIter end);
int main()//from   w  w w.  j  ava 2s .c om
{
   vector<char> v;
   for(int i=0; i < 26; i++)
      v.push_back('A'+i);
   show_range("Original ordering of v:\n", v.begin(), v.end());
   cout << endl;

   sort(v.begin(), v.end(), not2(less<char>()));
   show_range("After sorting v using not2(less<char>()):\n", v.begin(), v.end());
   cout << endl;

   vector<char>::iterator res_end;
   res_end = remove_if(v.begin(), v.end(), not1(bind2nd(equal_to<char>(), 'H')));
   show_range("v after removing elements not equal to H:\n", v.begin(), res_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