C++ std::max_element() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

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.

C++
std::max_element(v.begin(), v.end());
std::max_element(v.begin(), v.end(), comp);
CallWhat 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
C++
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

ParameterDescription
first, lastRequired. The range to search.
compOptional. A function that returns true when its first argument is smaller than the second.

Return Value

Return ValueDescription
IteratorPoints at the largest element (the first one if there is a tie).
lastReturned when the range is empty.

C++ Examples

Example 1 — Largest value and index

Find the biggest number and where it is.

C++
#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

  1. The function looks at every element and remembers the biggest.
  2. 9 is the largest, at index 2.
  3. *it gives the value.

Example 2 — Best student

Find the object with the highest score.

C++
#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

  1. The lambda decides that a student is "smaller" when the score is lower.
  2. Ben has the highest score, 95.
  3. best->name reads the name from the matching object.

Example 3 — Search part of a vector

Limit the search to the first three elements.

C++
#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;
}

How It Works

  1. v.begin() + 3 stops the range before index 3.
  2. Only 2, 8 and 3 are checked.
  3. 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.

Next: merge()

Merges two sorted ranges into one sorted range.

merge() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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