C++ algorithm min_element() function
Example
Find the lowest value in a vector:
vector<int> numbers = {1, 7, 3, 5, 9, 2};
auto it = min_element(numbers.begin(), numbers.end());
if (it != numbers.end()) {
cout << *it << " is the lowest value";
} else {
cout << "The vector is empty";
}
Try it Yourself »
Definition and Usage
The min_element()
function returns an iterator pointing to the element with the lowest value in a data range.
The range of data is specified by iterators.
Syntax
min_element(iterator start, iterator end);
Parameter Values
Parameter | Description |
---|---|
start | Required. An iterator pointing to the start of the data range. |
end | Required. An iterator pointing to the end of the data range. Elements up to this position will be included, but the element at this position will not be. |
Technical Details
Returns: | An iterator pointing to the element in the data range that has the lowest value. If the data range is empty it returns the end of the data range. |
---|
Related Pages
Read more about data structures in our Data Structures Tutorial.
Read more about iterators in our Iterators Tutorial.
Read more about algorithms in our Algorithms Tutorial.