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 containers like vector, set, etc.
Header File:
#include <functional.h>
Template Class:
template <class T> struct less_equal {
// Declaration of the
// less 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;
};
Syntax:
std::bind2nd(std::less_equal(), K)
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 less than equals to b.
- False: If a is greater than b.
Below is the illustration of std::less_equal in C++:
Program 1:
// C++ program to illustrate less_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 increasing order sort(arr, arr + N, less_equal<int>()); // Print sorted array printArray(arr, N); return 0; } |
0 1 2 3 4 5 6 7 8 9
Program 2:
// C++ program to illustrate less_equal #include <functional> #include <iostream> #include <iterator> #include <set> using namespace std; // Driver Code int main() { // Initialise set with less_equal to // store the elements in increasing // order set<int, less_equal<int> > S; // Insert elements in S S.insert(75); S.insert(30); S.insert(60); S.insert(20); S.insert(50); S.insert(30); // Print the elements stored in set S for (auto& it : S) { cout << it << ' '; } return 0; } |
20 30 30 50 60 75
Reference: http://www.cplusplus.com/reference/functional/less_equal/
Recommended Posts:
- cin get() in C++ with Examples
- std::less in C++ with Examples
- Pi(π) in C++ with Examples
- std::bit_xor in C++ with Examples
- iswgraph() in C/C++ with Examples
- SDL library in C/C++ with examples
- std::greater_equal in C++ with Examples
- ratio_not_equal() in C++ with examples
- iswprint() in C/C++ with Examples
- std::to_address in C++ with Examples
- ios eof() function in C++ with Examples
- ios bad() function in C++ with Examples
- std::bit_or in C++ with Examples
- set_symmetric_difference in C++ with Examples
- C/C++ while loop with Examples
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.

