c++11: less的用法


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

 

 

  


 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM