std::less in C++ with Examples

The std::less is a is a member of the functional class (<functional.h>) used for performing comparisons. It is defined as a function object class for less than inequality comparison which returns a boolean value depending upon the condition. This can be used to change the functionality of the given function. It can be used with various standard algorithms like sort, lower_bound etc.

Header File:

#include <functional.h>

Template Class:

template <class T> struct less {

  // Declaration of the less 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::less()

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 b.
  • False: If a is greater than b.

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

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// std::less function
#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;
  
// Function to print array arr[]
void printArray(int arr[], int N)
{
  
    for (int i = 0; i < N; i++) {
        cout << arr[i] << ' ';
    }
}
  
// Driver Code
int main()
{
    int arr[] = { 26, 23, 21, 22,
                  28, 27, 25, 24 };
  
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Sort the array in increasing order
    sort(arr, arr + N, less<int>());
  
    // Print sorted array
    printArray(arr, N);
    return 0;
}

chevron_right


Output:

21 22 23 24 25 26 27 28

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate less
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
  
// Template
template <typename A, typename B, 
          typename U = std::less<int> >
  
// Function to check if a < b or not
bool f(A a, B b, U u = U())
{
    return u(a, b);
}
  
// Driver Code
int main()
{
    int X = 1, Y = 2;
  
    // If X is less than Y or not
    cout << std::boolalpha;
    cout << f(X, Y) << '\n';
  
    X = 2, Y = -1;
  
    // If X is less than Y or not
    cout << f(X, Y) << '\n';
  
    return 0;
}

chevron_right


Output:

true
false

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

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 :


2


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