stl_algorithm算法之Max/Min算法

Min/max:

7.71、template <class T> 
const T& min (const T& a, const T& b) 
{
  return !(b<a)?a:b; // or: return !comp(b,a)?a:b; for version (2)
}

 

7.72、template <class T> 
const T& max (const T& a, const T& b)
{
  return (a<b)?b:a; // or: return comp(a,b)?b:a; for version (2)
}

 

7.73、template <class T> 
pair <const T&,const T&> minmax (const T& a, const T& b) 
{
  return (b<a) ? std::make_pair(b,a) : std::make_pair(a,b);
}

 

复制代码
7.74、template <class ForwardIterator>
ForwardIterator min_element ( ForwardIterator first, ForwardIterator last )
{
  if (first==last) return last;
  ForwardIterator smallest = first;
  while (++first!=last)
    if (*first<*smallest) // or: if (comp(*first,*smallest)) for version (2)
      smallest=first;
  return smallest;
}
//返回区间中最小的元素。
复制代码

 

复制代码
7.75、template <class ForwardIterator>
ForwardIterator max_element ( ForwardIterator first, ForwardIterator last )
{
  if (first==last) return last;
  ForwardIterator largest = first;
  while (++first!=last)
    if (*largest<*first) // or: if (comp(*largest,*first)) for version (2)
      largest=first;
  return largest;
}
//返回区间中最大的元素。

 

posted @ 2017-05-01 13:09  _xiaohaige  阅读(1271)  评论(0编辑  收藏  举报