C++ Tutorial/STL Algorithms Min Max/max

Материал из C\C++ эксперт
Версия от 10:30, 25 мая 2010; Admin (обсуждение | вклад) (1 версия: Импорт контента...)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Call max() with special comparison function

/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference"
 * by Nicolai M. Josuttis, Addison-Wesley, 1999
 *
 * (C) Copyright Nicolai M. Josuttis 1999.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <algorithm>
#include <iostream>
using namespace std;
/* function that compares two pointers by comparing the values to which they po
int
 */
bool int_ptr_less (int* a, int* b)
{
    return *a < *b;
}
int main()
{
    int x = 17;
    int y = 42;
    int* px = &x;
    int* py = &y;
    int* pmax;
    // call max() with special comparison function
    pmax = max (px, py, int_ptr_less);
    cout << *pmax;
    //...
}
42

max

#include <iostream>
using std::cout;
using std::endl;
#include <algorithm>
int main()
{
   cout << "\nThe maximum of "G" and "Z" is: " << std::max( "G", "Z" );
   cout << "\nThe maximum of 12 and 7 is: " << std::max( 12, 7 );
   cout << endl; 
   return 0;
}
The maximum of "G" and "Z" is: Z
The maximum of 12 and 7 is: 12

min and max

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
   cout << "The minimum of 12 and 7 is: " << min( 12, 7 );
   cout << "\nThe maximum of 12 and 7 is: " << max( 12, 7 );
   cout << "\nThe minimum of "G" and "Z" is: " 
        << min( "G", "Z" );
   cout << "\nThe maximum of "G" and "Z" is: " 
        << max( "G", "Z" ) << endl;
   return 0;
}