Returns the element with the smallest value
You're viewing an older version of this page (#4187). View the current version.
View versions (1)
Definition
The min_element() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.
Interface
#include <algorithm>
template < class ForwardIterator >
ForwardIterator min_element(
ForwardIterator first,
ForwardIterator last
);
template < class ForwardIterator, class BinaryPredicate >
ForwardIterator min_element(
ForwardIterator first,
ForwardIterator last,
BinaryPredicate comp
);
Parameters:
| Parameter | Description |
| first | A forward iterator addressing the position of the first element in the range to be searched for the largest element |
| last | A forward iterator addressing the position one past the final element in the range to be searched for the largest element |
| comp | User-defined predicate function object that defines the sense in which one element is greater than another. The binary predicate takes two arguments and should return true when the first element is less than the second element and false otherwise |
Description
Min_element finds the smallest element in the range [first, last).
The first version compares objects using operator< and the second compares objects using a function object comp.
Return Value
Returns the lowest element in the range [first, last).
Complexity
The complexity is linear; performs exactly (last - first) - 1 comparisons.
References
Example 1
ProblemThis program illustrates the use of the STL min_element() algorithm (default version) to find the minimum value in a vector of integers.
Workings#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int a[] = {8, 6, 10, 2, 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 <<"\nThe minimum value in the vector is "
<<*min_element(v.begin(), v.end())<<".";
return 0;
}
SolutionOutput:
Here are the integer values in the vector:
8 6 10 2 4
The minimum value in the vector is 2.
Example 2
ProblemThis program illustrates the use of the STL min_element() algorithm (extended version) to find the minimum value in a vector of integers, when one integer is larger than another if and only if it has a greater digit sum.
Workings#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/* Tests if one integer has a smaller digit sum than another.
i1 and i2 have been initialized and i1, i2 are both > 0.
Returns true if sum of the digits in i1 is < sum of digits in i2, and otherwise returns false. */
bool hasSmallerDigitSum(int i1, int i2)
{
int digitSum1 = 0;
while (i1 != 0)
{
digitSum1 += i1 % 10;
i1 /= 10;
}
int digitSum2 = 0;
while (i2 != 0)
{
digitSum2 += i2 % 10;
i2 /= 10;
}
return digitSum1 < digitSum2;
}
int main()
{
int a[] = {39, 49, 110, 81, 47};
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 <<"\nThe minimum value (the value with the smallest digit sum) is "
<<*min_element(v.begin(), v.end(), hasSmallerDigitSum)<<".";
return 0;
}
SolutionOutput:
Here are the integer values in the vector:
39 49 110 81 47
The minimum value (the value with the smallest digit sum) is 110.
See Also