類模板
<functional>

std::less

template <class T> struct less;
函式物件類,用於小於比較
二元函式物件類,其呼叫返回其第一個引數是否小於第二個引數(由 operator < 返回)。

泛指,函式物件 是一個類的例項,該類定義了成員函式operator()。這個成員函式允許物件以與函式呼叫相同的語法使用。

它的定義與以下行為相同:

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

此類物件可用於標準演算法,例如 sortmergelower_bound

模板引數

T
函式呼叫中用於比較的引數型別。
該型別應支援操作(operator<)。

成員型別

成員型別定義說明
first_argument_typeT成員 operator() 的第一個引數的型別
second_argument_typeT成員 operator() 的第二個引數的型別
result_typebool成員 operator() 返回的型別

成員函式

bool operator() (const T& x, const T& y)
成員函式,返回第一個引數是否小於第二個引數(x<y)。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// less example
#include <iostream>     // std::cout
#include <functional>   // std::less
#include <algorithm>    // std::sort, std::includes

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

輸出

foo includes bar.


另見