C++ std::max_element() Function

Definition
In C++, std::max_element() scans a range and returns an iterator to the largest element. If several elements tie, it returns the first one.
It is the quickest way to get the biggest value in a vector together with its position.
Syntax
Add #include <algorithm>, then call std::max_element(). Start with these two lines.
std::max_element(v.begin(), v.end());
std::max_element(v.begin(), v.end(), comp);
| Call | What it does |
|---|
std::max_element(v.begin(), v.end()); | Iterator to the largest element. |
std::max_element(v.begin(), v.end(), comp); | Use your own rule for "smaller". |
Library declarations
template<class ForwardIt>
ForwardIt max_element(ForwardIt first, ForwardIt last);
template<class ForwardIt, class Compare>
ForwardIt max_element(ForwardIt first, ForwardIt last, Compare comp);
Parameters
| Parameter | Description |
|---|
| first, last | Required. The range to search. |
| comp | Optional. A function that returns true when its first argument is smaller than the second. |
Return Value
| Return Value | Description |
|---|
| Iterator | Points at the largest element (the first one if there is a tie). |
last | Returned when the range is empty. |
C++ Examples
Example 1 — Largest value and index
Find the biggest number and where it is.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {7, 3, 9, 1, 5};
auto it = std::max_element(v.begin(), v.end());
std::cout << "Largest: " << *it << " at index " << (it - v.begin()) << '\n';
return 0;
}
How It Works
- The function looks at every element and remembers the biggest.
- 9 is the largest, at index 2.
*it gives the value.
Example 2 — Best student
Find the object with the highest score.
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
struct Student {
std::string name;
int score;
};
int main() {
std::vector<Student> list = {{"Asha", 72}, {"Ben", 95}, {"Chen", 85}};
auto best = std::max_element(list.begin(), list.end(),
[](const Student& a, const Student& b) { return a.score < b.score; });
std::cout << "Top student: " << best->name << '\n';
return 0;
}
How It Works
- The lambda decides that a student is "smaller" when the score is lower.
- Ben has the highest score, 95.
best->name reads the name from the matching object.
Example 3 — Search part of a vector
Limit the search to the first three elements.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {2, 8, 3, 9, 1};
auto it = std::max_element(v.begin(), v.begin() + 3);
std::cout << "Largest in first three: " << *it << '\n';
return 0;
}
Largest in first three: 8
How It Works
v.begin() + 3 stops the range before index 3.- Only 2, 8 and 3 are checked.
- The bigger 9 is outside the range, so 8 wins.
Frequently Asked Questions
In C++, std::max_element() finds the largest element in a range. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns an iterator to the largest element, or the last iterator if the range is empty.
Dereference the result: *std::max_element(v.begin(), v.end()). Check the range is not empty first.
The first largest element.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
18 people found this page helpful