C++ algorithm max_element() function
Example
Find the highest value in a vector:
vector<int> numbers = {1, 7, 3, 5, 9, 2};
auto it = max_element(numbers.begin(), numbers.end());
if (it != numbers.end()) {
cout << *it << " is the highest value";
} else {
cout << "The vector is empty";
}
Try it Yourself »
Definition and Usage
The max_element()
function returns an iterator pointing to the element with the highest value in a data range.
The range of data is specified by iterators.
Syntax
max_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 highest 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.