Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Algorithms
  • Analysis of Algorithms
  • Sorting
  • Searching
  • Greedy
  • Recursion
  • Backtracking
  • Dynamic Programming
  • Divide and Conquer
  • Geometric Algorithms
  • Mathematical Algorithms
  • Pattern Searching
  • Bitwise Algorithms
  • Branch & Bound
  • Randomized Algorithms
Open In App
Next Article:
std::greater in C++ with Examples
Next article icon

std::greater in C++ with Examples

Last Updated : 22 Apr, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
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: CPP
// 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; } 
Output:
  90 80 70 60 50 40 30 20 10  
Program 2: CPP
// 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; } 
Output:
  The priority queue gquiz is : 1 5 10 20 30  

Next Article
std::greater in C++ with Examples

K

kothariji
Improve
Article Tags :
  • Algorithms
  • C++ Programs
  • C++
  • Write From Home
  • DSA
Practice Tags :
  • CPP
  • Algorithms

Similar Reads

    std::greater_equal in C++ with Examples
    The std::greater_equal is a member of the functional class (<functional.h>). It is used to generate results of comparisons that are similar to the operator(≥). The advantage of this function over the operator(≥) is that it use the strict total order to generate the result, as opposed to
    2 min read
    std::less_equal in C++ with Examples
    The std::less_equals is a function object class used for performing comparisons. It is defined as a function object class for less than equality comparison, which returns a boolean value depending upon the condition. It can be used with various standard algorithms like sort, lower_bound and containe
    2 min read
    How to Use Custom Comparator with Set in C++?
    In C++, sets are associative containers that store unique elements in some sorted order. By default, set store data in increasing order but we can change this using a custom comparator. In this article, we will learn, how to declare a set with a custom comparator in C++ STL. Example Input: Data = {1
    2 min read
    How to Overload the Less-Than (<) Operator in C++?
    In C++ we have an operator called less than operator (<) which checks if the left side operand is smaller than the right side operand or not. In this article, we will learn how to overload the less-than operator in C++. Overloading Less-Than Operator in C++In C++, we can overload the less-than op
    2 min read
    Relational Operators on STL Array in C++
    The article illustrates the working of the different relational operator on the array STL. The equality comparison ( == ) is performed by comparing the elements sequentially using operator ( == ), stopping at the first mismatch. The less-than comparison ( < ) or greater-than comparison ( > ) b
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences