std::greater in C++ with Examples

The std::greater is a functional object which is used for performing comparisons. It is defined as a Function object class for the greater-than inequality comparison. This can be used for changing the functionality of the given function. This can also be used with various standard algorithms such as sort, priority queue, etc.

Header File:

#include <functional.h>

Template Class:

template <class T> struct greater;

Parameter: T is a type of the arguments to compare by the functional call.

Return Value: It returns boolean variable as shown below:



  • True: If two element say(a & b) such that a > b.
  • False: If a < b.

Below is the illustration of std::greater in C++:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate std::greater
  
#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;
  
// Function to print array elements
void printArray(int arr[], int n)
{
  
    for (int i = 0; i < n; i++) {
        cout << arr[i] << ' ';
    }
}
  
// Driver Code
int main()
{
  
    int arr[] = { 60, 10, 80, 40, 30,
                  20, 50, 90, 70 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // To sort the array in decreasing order
    // use greater <int>() as an third arguments
    sort(arr, arr + 9, greater<int>());
  
    // Print array elements
    printArray(arr, n);
  
    return 0;
}

chevron_right


Output:

90 80 70 60 50 40 30 20 10

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate std::greater
  
#include <functional>
#include <iostream>
#include <queue>
using namespace std;
  
// Function to print elements of priority_queue
void showpq(priority_queue<int, vector<int>,
                           greater<int> >
                pq)
{
    priority_queue<int,
                   vector<int>,
                   greater<int> >
        g;
    g = pq;
  
    // While priority_queue is not empty
    while (!g.empty()) {
  
        // Print the top element
        cout << g.top() << ' ';
  
        // Pop the top element
        g.pop();
    }
}
  
// Driver Code
int main()
{
    // priority_queue use to implement
    // Max Heap, but using function
    // greater <int> () it implements
    // Min Heap
    priority_queue<int, vector<int>,
                   greater<int> >
        gquiz;
  
    // Inserting Elements
    gquiz.push(10);
    gquiz.push(30);
    gquiz.push(20);
    gquiz.push(5);
    gquiz.push(1);
  
    // Print elements of priority queue
    cout << "The priority queue gquiz is : ";
    showpq(gquiz);
    return 0;
}

chevron_right


Output:

The priority queue gquiz is : 1 5 10 20 30

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Code Enthusiast

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


11


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.