greater [C++ Reference]
Keywords: stl,functional,greater
Date: 2010-03-17T15:51:12-0700

[[greater]]
C++ Reference
Translations of this page?:

- en
- br-pt
- cn
- cz
- de
- es
- fr
- it
- jp
- nl
- pl
- ro
- ru
- sk
- tr
- tw

# greater

##### template <class T> struct greater;

Function object class for greater-than inequality comparison

This class defines function objects for the “greater than” inequality comparison operation.

Generically, function objects are instances of a class with member function operator() defined. This member function allows the object to be used with the same syntax as a regular function call, and therefore it can be used in templates instead of a pointer to a function.

greater has its operator() member defined such that it returns true if its first argument compares greater than the second one using operator>, and false otherwise.

This class is derived from binary_function and is defined as:

    template <class T> struct greater : binary_function <T,T,bool> {
      bool operator() (const T& x, const T& y) const
        {return x>y;}
    };

Objects of this class can be used with some standard algorithms such as sort, merge or lower_bound.

### **Members**

T operator() (const T& x, const T& y) Member function returning the result of the comparison x>y.

#### // greater example

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

    int main () {
      int numbers[]={20,40,50,10,30};
      sort (numbers, numbers+5, greater<int>() );
      for (int i=0; i<5; i++)
        cout << numbers[i] << " ";
      cout << endl;
      return 0;
    }

Output:

    50 40 30 20 10

Edit this page • Old revisions
• • • Login • Index • Recent changes • RSS • cc •