C++STL中的less和greater

廉价感情. 提交于 2019-12-24 06:36:36

greater() 和less()函数经常使用在sort()中用来对容器进行降序或者升序排序,或者用在push_heap()和pop_heap()中用来构建最小堆(greater)或者最大堆(less).
二者包含在头文件functional中

//包含在头文件<functional>中

   // TEMPLATE STRUCT greater  
template<class _Ty>  
struct greater  
   : public binary_function<_Ty, _Ty, bool>  
{   // functor for operator>  
bool operator()(const _Ty& _Left, const _Ty& _Right) const  
   {   // apply operator> to operands  
   return (_Left > _Right);  
   }  
};  

   // TEMPLATE STRUCT less  
template<class _Ty>  
struct less  
   : public binary_function<_Ty, _Ty, bool>  
{   // functor for operator<  
bool operator()(const _Ty& _Left, const _Ty& _Right) const  
   {   // apply operator< to operands  
   return (_Left < _Right);  
   }  
};  
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!