Demonstrate bind2nd() : bind2nd « STL Algorithms Helper « C++






Demonstrate bind2nd()

Demonstrate bind2nd()
  
 

#include <iostream>
#include <list>
#include <functional>
#include <algorithm>
using namespace std;

int main()
{
  list<int> lst;
  list<int>::iterator p, endp;

  for(int i=1; i < 20; i++) lst.push_back(i);

  cout << "Original sequence:\n";
  p = lst.begin();
  while(p != lst.end()) {
    cout << *p << " ";
    p++;
  }
  cout << endl;

  endp = remove_if(lst.begin(), lst.end(),
                   bind2nd(greater<int>(), 8));

  cout << "Resulting sequence:\n";
  p = lst.begin();
  while(p != endp) {
    cout << *p << " ";
    p++;
  }

  return 0;
}

/* 
Original sequence:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Resulting sequence:
1 2 3 4 5 6 7 8 
 */        
    
  








Related examples in the same category

1.Use the generic count algorithm with predicate: Determine the number of array elements that are not equal to 1
2.transform, bind2nd and modulus
3.transform, bind2nd and plus
4.multiply by 100 to get a percent
5.Count vowels in a sentence