c++11: less的用法

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-21 17:19   48   0

less主要是重载了operator()方法,用来比较lhs 和 rhs

std::less::operator()

bool operator()(const T &lhs, const T &rhs) const;

constexpr bool operator()(const T &lhs, const T &rhs) const ;

内部实现:

constexpr bool operator()(const T &lhs, const T &rhs) const

{

  return lhs < rhs;

}

如果lhs比rhs小,就返回true;如果lhs比rhs大,就返回false;

例子:

#include <functionl>
#include <iostream>
using namespace std;
template <typename A, typename B, typename U = less<int>>
bool m(A a, B b, U u = U())
{
  return u(a,b);
}

int main()
{
  cout << less<int>()(10, 12) <<eendl;
  cout << less<int>()(12, 10) << endl;
  cout << m(10, 12) << endl;
}

输出结果:

true

false

true

  


转载于:https://www.cnblogs.com/457220157-FTD/p/4018742.html

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP