std::min_element

Header: <algorithm>

Finds the smallest element in the range [first,last).

# Declarations

template< class ForwardIt >
ForwardIt min_element( ForwardIt first, ForwardIt last );

(constexpr since C++17)

template< class ExecutionPolicy, class ForwardIt >
ForwardIt min_element( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last );

(since C++17)

template< class ForwardIt, class Compare >
ForwardIt min_element( ForwardIt first, ForwardIt last,
Compare comp );

(constexpr since C++17)

template< class ExecutionPolicy, class ForwardIt, class Compare >
ForwardIt min_element( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last,
Compare comp );

(since C++17)

# Parameters

# Return value

Iterator to the smallest element in the range [first,last). If several elements in the range are equivalent to the smallest element, returns the iterator to the first such element. Returns last if the range is empty.

# Example

#include <algorithm>
#include <iostream>
#include <vector>
 
int main()
{
    std::vector<int> v{3, 1, -4, 1, 5, 9};
 
    std::vector<int>::iterator result = std::min_element(v.begin(), v.end());
    std::cout << "min element has value " << *result << " and index ["
              << std::distance(v.begin(), result) << "]\n";
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 212C++98the return value was not specified if [first, last) is emptyreturns last in this case
LWG 2150C++98the iterator to the first non-greatest element was returnedcorrected the return value

# See also