当前位置:编程学习 > C/C++ >>

max_element/min_element详解

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
   
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,