Home C&C++函数库 c++ 语法 程序源码 Linux C库

std::

less

类模板  <functional>

template <class T> struct less;

用于小于不等式比较的函数对象类
二元函数对象类,其调用将返回其第一个参数的比较是否小于第二个参数(由操作符<返回)。

一般来说,函数对象是定义了成员函数operator()的类的实例。该成员函数允许使用与函数调用相同的语法来使用对象。

它的行为定义如下:

C++98
template <class T> struct less : binary_function <T,T,bool> {
  bool operator() (const T& x, const T& y) const {return x<y;}
};


该类的对象可用于sort, merge 或 lower_bound等标准算法。

☲  模板参数


T
函数调用要比较的参数的类型。
类型应支持操作(operator<)。

☲  成员类型


成员类型 定义 注释
first_argument_type T 成员operator()中第一个参数的类型
second_argument_type T 成员operator()中第二个参数的类型
result_type bool 成员operator()返回的类型

☲  成员函数



T operator() (const T& x, const T& y)
成员函数返回第一个参数是否比第二个参数大(x<y)。

☣  示例



// 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.

🍄  另请参阅



equal_to 用于相等比较的函数对象类(类模板)
not_equal_to 用于非相等比较的函数对象类(类模板)
greater 用于大于不等比较的函数对象类(类模板)
greater_equal 用于大于等于比较的函数对象类(类模板)
less_equal 用于小于等于比较的函数对象类(类模板)
binary_function 二元函数对象基类 (类模板)

联系我们 免责声明 关于CandCplus 网站地图