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 the operator(≥) which uses the partial order. It returns a boolean value depending upon the condition. It can be used for comparing integers, characters, or strings, etc.

Header File:

#include <functional.h>

Template Class:

template <class T> struct greater_equal {

  // Declaration of the
  // greater equal operation 
  bool operator() (const T& x, const T& y) 
       const 
  {
      return x>=y;
  }

  // Type of first parameter
  typedef T first_argument_type;

  // Type of second parameter
  typedef T second_argument_type;

  // The result is returned
  // as bool type
  typedef bool result_type;
};

Parameter: This function accepts the type of the arguments T, as the parameter, to be compared by the functional call.

Return Type: It return a boolean value depending upon condition (let a & b are 2 element):



  • True: If a is greater than equals to b.
  • False: If a is less than b.

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

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate greater_equal
  
#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;
  
// Function to print the array arr[]
void printArray(int arr[], int N)
{
  
    for (int i = 0; i < N; i++) {
        cout << arr[i] << ' ';
    }
}
  
// Driver Code
int main()
{
    int arr[] = { 1, 5, 8, 9, 6,
                  7, 3, 4, 2, 0 };
  
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Sort the array in decreasing order
    sort(arr, arr + N, greater_equal<int>());
  
    // Print sorted array
    printArray(arr, N);
    return 0;
}

chevron_right


Output:

9 8 7 6 5 4 3 2 1 0

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate greater_equal
  
#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;
  
// Driver Code
int main()
{
  
    int arr[] = { 30, 40, -50, 60, -70,
                  10, 20, -80, 90, 100 };
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Print the number of elements less
    // than 0
    cout << count_if(
        arr, arr + N,
        bind2nd(greater_equal<int>(),
                0));
    return 0;
}

chevron_right


Output:

7

Reference: http://www.cplusplus.com/reference/functional/greater_equal/

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 :


Be the First to upvote.


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