max_element/min_element:
max_element算法:
template<class_FwdIt> inline
_FwdIt _Max_element(_FwdIt _First,_FwdIt _Last)
{ // find largest element, using operator<
_FwdIt _Found = _First;
if(_First != _Last)
for(; ++_First != _Last; )
if (_DEBUG_LT(*_Found, *_First))
_Found =_First;
return(_Found);
}
template<class_FwdIt> inline
_FwdIt max_element(_FwdIt _First,_FwdIt _Last)
{ // find largest element, using operator<
_DEBUG_RANGE(_First, _Last);
return(_Rechecked(_First,
_Max_element(_Unchecked(_First),_Unchecked(_Last))));
}
其中_DEBUG_LT宏表示如下:
#ifndef_DEBUG_LT_IMPL
#define_DEBUG_LT_IMPL _Debug_lt
#endif /* _DEBUG_LT_IMPL */
#define_DEBUG_LT(x, y) \
_DEBUG_LT_IMPL(x, y, _FILENAME,__LINE__)
template<class _Ty1, class _Ty2> inline
bool_Debug_lt(_Ty1& _Left, _Ty2& _Right,
_Dbfile_t _File, _Dbline_t_Line)
{ // test if _Left < _Right and operator< is strictweak ordering
if(!(_Left < _Right))
return(false);
else if (_Right < _Left)
_DEBUG_ERROR2("invalid operator<", _File, _Line);
return(true);
}
函数功能:返回区间最大值
对应的一个函数(min_element)
//TEMPLATE FUNCTION min_element
template<class_FwdIt> inline
_FwdIt _Min_element(_FwdIt _First,_FwdIt _Last)
{ // find smallest element, using operator<
_FwdIt _Found = _First;
if(_First != _Last)
for(; ++_First != _Last; )
if (_DEBUG_LT(*_First, *_Found))
_Found =_First;
return(_Found);
}
template<class_FwdIt> inline
_FwdIt min_element(_FwdIt _First,_FwdIt _Last)
{ // find smallest element, using operator<
_DEBUG_RANGE(_First, _Last);
return(_Rechecked(_First,
_Min_element(_Unchecked(_First),_Unchecked(_Last))));
}
函数功能:返回区间最小值
对应的两个算法(以仿函数或bool (fun*)(T,T) )
template<class _FwdIt,
class_Pr> inline
_FwdIt _Min_element(_FwdIt _First,_FwdIt _Last, _Pr _Pred)
{ // find smallest element, using _Pred
_FwdIt _Found = _First;
if(_First != _Last)
for(; ++_First != _Last; )
if (_DEBUG_LT_PRED(_Pred, *_First, *_Found))
_Found =_First;
return(_Found);
}
template<class _FwdIt,
class_Pr> inline
_FwdIt min_element(_FwdIt _First,_FwdIt _Last, _Pr _Pred)
{ // find smallest element, using _Pred
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER(_Pred);
return(_Rechecked(_First,
_Min_element(_Unchecked(_First),_Unchecked(_Last), _Pred)));
}
其中_DEBUG_LT_PRED宏表示为:
#ifndef _DEBUG_LT_PRED_IMPL
#define_DEBUG_LT_PRED_IMPL _Debug_lt_pred
#endif /* _DEBUG_LT_PRED_IMPL */
#define_DEBUG_LT_PRED(pred, x, y) \
_DEBUG_LT_PRED_IMPL(pred, x, y,_FILENAME, __LINE__)
template<class _Pr, class _Ty1, class_Ty2> inline
bool_Debug_lt_pred(_Pr _Pred,
_Ty1& _Left, _Ty2&_Right,
_Dbfile_t _File, _Dbline_t_Line)
{ // test if _Pred(_Left, _Right) and _Pred is strict weakordering
if(!_Pred(_Left, _Right))
return(false);
else if (_Pred(_Right, _Left))
_DEBUG_ERROR2("invalid operator<", _File, _Line);
return(true);
}
函数功能:使得_Pred返回true的最小值
对应max_element:
template<class _FwdIt,
class_Pr> inline
_FwdIt _Max_element(_FwdIt _First,_FwdIt _Last, _Pr _Pred)
{ // find largest element, using _Pred
_FwdIt _Found = _First;
if(_First != _Last)
for(; ++_First != _Last; )
if (_DEBUG_LT_PRED(_Pred, *_Found, *_First))
_Found =_First;
return(_Found);
}
template<class _FwdIt,
class_Pr> inline
_FwdIt max_element(_FwdIt _First,_FwdIt _Last, _Pr _Pred)
{ // find largest element, using _Pred
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER(_Pred);
return(_Rechecked(_First,
_Max_element(_Unchecked(_First),_Unchecked(_Last), _Pred)));
}
函数功能:使得_Pred返回true的最小值
注意:无论使用哪个函数,我们都不要通过函数来改变区间的值.
举例:
template<typenameT>
bool retMaxValue( T _value1,T _value2 )
{
returnabs( _value1 ) < abs( _value2 );
}
int main()
{
vector<int>vecInt;
vecInt.push_back( 2 );
vecInt.push_back( 5 );
vecInt.push_back( 7 );
vecInt.push_back( 3 );
vecInt.push_back( 2 );
vecInt.push_back( 4 );
vecInt.push_back( 3 );
vecInt.push_back( -17 );
vecInt.push_back( 3 );
cout<<*max_element(vecInt.begin(),vecInt.end() )<<"\n";
cout<<*max_element(vecInt.begin(),vecInt.end(),retMaxValue<int>)<<"\n";
cout<<*min_element(vecInt.begin(),vecInt.end(),retMaxValue<int>)<<"\n";
system( "pause");
return0;
}
说明:无论是max_element/min_element_Pred定义的都是_First<_Last,只是通过传递两个参数不同顺序才得以实现最大最小值.