>>59645539вот тебе шаблонная ф-я
прямиком из буста:
#include &t;utility> // for std::pair and std::make_pair
namespace boost {
namespace detail { // for obtaining a uniform version of minmax_element
// that compiles with VC++ 6.0 -- avoid the iterator_traits by
// having comparison object over iterator, not over dereferenced value
template &t;typename Iterator>
struct less_over_iter {
bool operator()(Iterator const∓ it1,
Iterator const∓ it2) const { return *it1 &t; *it2; }
};
template &t;typename Iterator, class BinaryPredicate>
struct binary_pred_over_iter {
explicit binary_pred_over_iter(BinaryPredicate const∓ p ) : m_p( p ) {}
bool operator()(Iterator const∓ it1,
Iterator const∓ it2) const { return m_p(*it1, *it2); }
private:
BinaryPredicate m_p;
};
// common base for the two minmax_element overloads
template &t;typename ForwardIter, class Compare >
std::pair&t;ForwardIter, ForwardIter>
basic_minmax_element(ForwardIter first, ForwardIter last, Compare comp)
{
if (first == last)
return std::make_pair(last, last);
ForwardIter min_result = first;
ForwardIter max_result = first;
// if only one element
ForwardIter second = first; ++second;
if (second == last)
return std::make_pair(min_result, max_result);
// treat first pair separately (only one comparison for first two elements)
ForwardIter potential_min_result = last;
if (comp(first, second))
max_result = second;
else {
min_result = second;
potential_min_result = first;
}
// then each element by pairs, with at most 3 comparisons per pair
first = ++second; if (first != last) ++second;
while (second != last) {
if (comp(first, second)) {
if (comp(first, min_result)) {
min_result = first;
potential_min_result = last;
}
if (comp(max_result, second))
max_result = second;
} else {
if (comp(second, min_result)) {
min_result = second;
potential_min_result = first;
}
if (comp(max_result, first))
max_result = first;
}
first = ++second;
if (first != last) ++second;
}
// if odd number of elements, treat last element
if (first != last) { // odd number of elements
if (comp(first, min_result)) {
min_result = first;
potential_min_result = last;
}
else if (comp(max_result, first))
max_result = first;
}
// resolve min_result being incorrect with one extra comparison
// (in which case potential_min_result is necessarily the correct result)
if (potential_min_result != last
∓∓ !comp(min_result, potential_min_result))
min_result = potential_min_result;
return std::make_pair(min_result, max_result);
}
} // namespace detail
template &t;typename ForwardIter>
std::pair&t;ForwardIter, ForwardIter>
minmax_element(ForwardIter first, ForwardIter last)
{
return detail::basic_minmax_element(first, last,
detail::less_over_iter&t;ForwardIter>() );
}
template &t;typename ForwardIter, class BinaryPredicate>
std::pair&t;ForwardIter, ForwardIter>
minmax_element(ForwardIter first, ForwardIter last, BinaryPredicate comp)
{
return detail::basic_minmax_element(first, last,
detail::binary_pred_over_iter&t;ForwardIter, BinaryPredicate>(comp) );
}
}.
работает так:
int main()
{
using namespace std;
list&t;int> L;
generate_n(front_inserter(L), 1000, rand);// в этот список добавляешь ЛЮБОЕ кол-во эл-тов
typedef list&t;int>::const_iterator iterator;
pair&t; iterator, iterator > result2 = boost::minmax_element(L.begin(), L.end());
cout &t;&t; "The smallest element is " &t;&t; *(result2.first) &t;&t; endl;
cout &t;&t; "The largest element is " &t;&t; *(result2.second) &t;&t; endl;
}