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