predicates (from functional.h)

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

int main () {
  int numbers[]={20,40,50,10,30};
  sort(numbers, numbers+5, greater<int>() ); //Result: 50 40 30 20 10
  //sort(numbers, numbers+5, less<int>() ); //Result: 10 20 30 40 50
  //sort(numbers, numbers+5, equal_to<int>() ); //Result: 20 40 50 10 30
  //sort(numbers, numbers+5, greater_equal<int>() ); //Result: 50 40 30 20 10
  //sort(numbers, numbers+5, less_equal<int>() ); //Result: 10 20 30 40 50
  for (int i=0; i<5; i++)
  {
    cout << numbers[i] << " ";
  }
  cout << endl;

  int numbers2[]={10,10,10,20,20};
  int* pt = adjacent_find( numbers2, numbers2 + 5, not_equal_to<int>() ) + 1;
  cout << "The first different element is " << *pt << endl;
  //Result: The first different element is 20

  return 0;
}


 

你可能感兴趣的:(Predicate)