greater() 和less()函数经常使用在sort()中用来对容器进行升序或者降序排序,或者用在push_heap()和pop_heap()中用来构建最小堆(greater)或者最大堆(less).
二者包含在头文件functional中
//包含在头文件<functional>中
// TEMPLATE STRUCT greater
emplate<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
emplate<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);
}
};