max_element/min_element详解

41 篇文章 0 订阅
39 篇文章 2 订阅

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,只是通过传递两个参数不同顺序才得以实现最大最小值.

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值