C++ Tutorial/STL Algorithms Min Max/max
Call max() with special comparison function
<source lang="cpp">/* 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; //...
}</source>
42
max
<source lang="cpp">#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;
}</source>
The maximum of "G" and "Z" is: Z The maximum of 12 and 7 is: 12
min and max
<source lang="cpp">#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;
}</source>