Min() 算法比较简单,它的作用是比较两个值大小然后返回最小值,可以先看下算法的定义:
// TEMPLATE FUNCTION min
template<class _Ty> inline
_Post_equal_to_(_Right < _Left ? _Right : _Left)
_CONST_FUN const _Ty& (min)(const _Ty& _Left, const _Ty& _Right)
{ // return smaller of _Left and _Right
return (_DEBUG_LT(_Right, _Left) ? _Right : _Left);
}
其中 _Debug_LT 的宏定义为
#define _DEBUG_LT(x, y) ((x) < (y))
即判断右值是否小于左值然后返回小值,它也有仿函数重载:
// TEMPLATE FUNCTION min WITH PRED
template<class _Ty,
class _Pr> inline
_CONST_FUN const _Ty& (min)(const _Ty& _Left, const _Ty& _Right,
_Pr _Pred)
{ // return smaller of _Left and _Right using _Pred
return (_DEBUG_LT_PRED(_Pred, _Right, _Left) ? _Right : _Left);
}
其中 _DEBUG_LT_PRED 的宏定义为
#define _DEBUG_LT_PRED(pred, x, y) pred(x, y)
可自定义仿函数处理两值,返回值不变仍为传入的数据类型。
而 min() 函数还有个初始化队列作参数的重载: