标签:algorithm 区间 turn log ons ref 复制 算法 while
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;
}
//返回区间中最大的元素。
标签:algorithm 区间 turn log ons ref 复制 算法 while
原文地址:http://www.cnblogs.com/xiaohaige/p/6791729.html