程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> max_element/min_element詳解

max_element/min_element詳解

編輯:C++入門知識

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,只是通過傳遞兩個參數不同順序才得以實現最大最小值.



摘自 yuanweihuayan的專欄

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved