min
Returns the lowest of its two arguments
You're viewing an older version of this page (#4186). View the current version.
Definition
The min() 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& min(
const Type& left,
const Type& right
);
template < class Type, class Pr >
const Type& min(
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
Min function compares two objects and finds the smaller of the two values.
The first version compares objects using operator< and the second compares objects using the function object comp.
Return Value
Returns the smaller of the two values.
Complexity
Constant complexity.
References
Example 1
This program illustrates the use of the STL min() algorithm (default version) to find the minimum of two integer literal values, the minimum of two integer values stored in simple integer variables, and (in two different ways) the minimum of two integer values stored in a vector of integers.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
cout <<"\nFirst, we find the minimum of two integer literal values.\n";
cout <<"min(12, -3) = "<<min(12, -3);
int first = 7;
int second = 10;
cout <<"\nNext, we find the minimum of two integer \nvalues stored in simple integer variables";
cout <<"\nfirst = "<<first<<" second = "<<second<<endl;
cout <<"min(first, second) = "<<min(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 minimum of the first and last values "
"\nstored in the vector of integers using one approach.";
cout <<"\nmin(v.at(0), v.at(4)) = "<<min(v.at(0), v.at(4));
cout <<"\nFinally, we find the minimum of the first and last values "
"\nstored in the vector of integers using a second approach.";
cout <<"\nmin(*v.begin(), *(v.end()-1)) = "<<min(*v.begin(), *(v.end()-1));
return 0;
}Output:
First, we find the mminimum of two integer literal values.
min(12, -3) = -3
Next, we find the minimum of two integer
values stored in simple integer variables
first = 7 second = 10
min(first, second) = 7
Here are the integer values in the vector:
2 6 10 8 4
Now we find the minimum of the first and last values
stored in the vector of integers using one approach.
min(v.at(0), v.at(4)) = 2
Finally, we find the minimum of the first and last values
stored in the vector of integers using a second approach.
min(*v.begin(), *(v.end()-1)) = 2
See Also
- http://www.codecogs.com/reference/computing/stl/algorithms/min_max/max.php"max"
- http://www.codecogs.com/reference/computing/stl/algorithms/min_max/max_element.php"max_element"
- http://www.codecogs.com/reference/computing/stl/algorithms/min_max/min_element.php"min_element"
- http://www.codecogs.com/reference/computing/stl/algorithms/sorting/sort.php"sort"