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);
}
};
本文详细介绍了greater()和less()函数的应用,这两种函数通常用于容器的排序操作,如sort(), push_heap() 和 pop_heap()等。greater()常用于构建最小堆,而less()则用于构建最大堆。二者都定义在<functional>头文件中。
1万+

被折叠的 条评论
为什么被折叠?



