class template

std::less

<functional>
template <class T> struct less;
Function object class for less-than inequality comparison
This class defines function objects for the "less than" inequality comparison operation.

Generically, function objects are instances of a class with member function operator() defined. This member function allows the object to be used with the same syntax as a regular function call, and therefore it can be used in templates instead of a pointer to a function.

less has its operator() member defined such that it returns true if its first argument compares lower than the second one using operator<, and false otherwise.

This class is derived from binary_function and is defined as:

1
2
3
4
template <class T> struct less : binary_function <T,T,bool> {
  bool operator() (const T& x, const T& y) const
    {return x<y;}
};


Objects of this class can be used with some standard algorithms such as sort, merge or lower_bound.

Members

bool operator() (const T& x, const T& y)
Member function returning the result of the comparison x<y.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// less example
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;

int main () {
  int foo[]={10,20,5,15,25};
  int bar[]={15,10,20};
  sort (foo, foo+5, less<int>() );	// 5 10 15 20 25
  sort (bar, bar+3, less<int>() );  //   10 15 20
  if ( includes ( foo, foo+5, bar, bar+3, less<int>() ) )
	  cout << "foo includes bar.\n";
  return 0;
}


Output:

foo includes bar.

See also