max
Returns the greatest of its two arguments
Key Facts
Gyroscopic Couple: The rate of change of angular momentum () = (In the limit).- = Moment of Inertia.
- = Angular velocity
- = Angular velocity of precession.
Blaise Pascal (1623-1662) was a French mathematician, physicist, inventor, writer and Catholic philosopher.
Leonhard Euler (1707-1783) was a pioneering Swiss mathematician and physicist.
Definition
The max() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.Interface
#include <algorithm> template < class Type > const Type& max( const Type& left, const Type& right ); template < class Type, class Pr > const Type& max( const Type& left, const Type& right, BinaryPredicate comp );Parameters:
Parameter | Description |
---|---|
left | The first of the two objects being compared |
right | The second of the two objects being compared |
comp | A binary predicate used to compare the two objects |
Description
Max function compares two objects and finds the grater of the two values. The first version compares objects usingoperator<
and the second compares objects using the function object comp
.References
Example:
Example - max algorithm
Problem
This program illustrates the use of the STL max() algorithm (default version)
to find the maximum of two integer literal values, the maximum of two integer
values stored in simple integer variables, and (in two different ways) the
maximum of two integer values stored in a vector of integers.
Workings
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { cout <<"\nFirst, we find the maximum of two integer literal values.\n"; cout <<"max(12, -3) = "<<max(12, -3); int first = 7; int second = 10; cout <<"\nNext, we find the maximum of two integer \nvalues stored in simple integer variables"; cout <<"\nfirst = "<<first<<" second = "<<second<<endl; cout <<"max(first, second) = "<<max(first, second) int a[] = {2, 6, 10, 8, 4}; vector<int> v(a, a+5); cout <<"\nHere are the integer values in the vector:\n"; for (vector<int>::size_type i=0; i<v.size(); i++) cout <<v.at(i)<<" "; cout <<"\nNow we find the maximum of the first and last values " "\nstored in the vector of integers using one approach."; cout <<"\nmax(v.at(0), v.at(4)) = "<<max(v.at(0), v.at(4)); cout <<"\nFinally, we find the maximum of the first and last values " "\nstored in the vector of integers using a second approach."; cout <<"\nmax(*v.begin(), *(v.end()-1)) = "<<max(*v.begin(), *(v.end()-1)); return 0; }
Solution
Output:
First, we find the maximum of two integer literal values.
max(12, -3) = 12 Next, we find the maximum of two integer
values stored in simple integer variables
first = 7 second = 10
max(first, second) = 10 Here are the integer values in the vector:
2 6 10 8 4 Now we find the maximum of the first and last values
stored in the vector of integers using one approach.
max(v.at(0), v.at(4)) = 4 Finally, we find the maximum of the first and last values
stored in the vector of integers using a second approach.
max(*v.begin(), *(v.end()-1)) = 4
max(12, -3) = 12 Next, we find the maximum of two integer
values stored in simple integer variables
first = 7 second = 10
max(first, second) = 10 Here are the integer values in the vector:
2 6 10 8 4 Now we find the maximum of the first and last values
stored in the vector of integers using one approach.
max(v.at(0), v.at(4)) = 4 Finally, we find the maximum of the first and last values
stored in the vector of integers using a second approach.
max(*v.begin(), *(v.end()-1)) = 4
References