minmax_ex.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // (C) Copyright Herve Bronnimann 2004.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #include <list>
  6. #include <algorithm>
  7. #include <cstdlib>
  8. #include <cassert>
  9. #include <iostream>
  10. #include <iterator>
  11. #include <boost/algorithm/minmax.hpp>
  12. #include <boost/algorithm/minmax_element.hpp>
  13. int main()
  14. {
  15. using namespace std;
  16. // Demonstrating minmax()
  17. boost::tuple<int const&, int const&> result1 = boost::minmax(1, 0);
  18. assert( result1.get<0>() == 0 );
  19. assert( result1.get<1>() == 1 );
  20. // Demonstrating minmax_element()
  21. list<int> L;
  22. typedef list<int>::const_iterator iterator;
  23. generate_n(front_inserter(L), 1000, rand);
  24. pair< iterator, iterator > result2 = boost::minmax_element(L.begin(), L.end());
  25. cout << "The smallest element is " << *(result2.first) << endl;
  26. cout << "The largest element is " << *(result2.second) << endl;
  27. assert( result2.first == std::min_element(L.begin(), L.end()) );
  28. assert( result2.second == std::max_element(L.begin(), L.end()) );
  29. }