C++ std::max() Function

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

Definition

In C++, std::max() compares values and returns the largest one. You can give it two values or a braced list.

With a custom comparison, you decide what "larger" means, for example the longer string.

Syntax

Add #include <algorithm>, then call std::max(). Start with these two lines.

C++
std::max(a, b);
std::max({4, 9, 2, 7});
CallWhat it does
std::max(a, b);The larger of two values.
std::max({4, 9, 2, 7});The largest value in a list.
Library declarations
C++
template<class T>
const T& max(const T& a, const T& b);

template<class T>
T max(std::initializer_list<T> list);

template<class T, class Compare>
const T& max(const T& a, const T& b, Compare comp);

Parameters

ParameterDescription
a, bRequired (two-value form). The values to compare.
listA braced list such as {4, 9, 2}. It must not be empty.
compOptional. A comparison function that returns true when its first argument is smaller than the second.

Return Value

Return ValueDescription
ReferenceThe two-value form returns a reference to the larger argument. If they are equal, it returns the first.
ValueThe list form returns a copy of the largest value.

C++ Examples

Example 1 — Two numbers

Pick the larger of two numbers.

C++
#include <algorithm>
#include <iostream>

int main() {
    int a = 3;
    int b = 8;

    int bigger = std::max(a, b);

    std::cout << "Bigger: " << bigger << '\n';
    return 0;
}

How It Works

  1. max(a, b) compares the two values.
  2. 8 is greater than 3, so it is returned.
  3. The result is copied into bigger.

Example 2 — Largest in a list

Pass many values in braces.

C++
#include <algorithm>
#include <iostream>

int main() {
    int largest = std::max({4, 9, 2, 7});

    std::cout << "Largest: " << largest << '\n';
    return 0;
}

How It Works

  1. The braces form an initializer list.
  2. max() checks each value and keeps the biggest.
  3. 9 is the largest of the four.

Example 3 — Longer word

Choose the longer of two strings.

C++
#include <algorithm>
#include <iostream>
#include <string>

int main() {
    std::string a = "cat";
    std::string b = "horse";

    std::string longer = std::max(a, b, [](const std::string& x, const std::string& y) {
        return x.size() < y.size();
    });

    std::cout << "Longer word: " << longer << '\n';
    return 0;
}

How It Works

  1. The lambda says x is "smaller" when it has fewer letters.
  2. cat has 3 letters and horse has 5.
  3. So horse counts as larger, and it is returned.

Frequently Asked Questions

In C++, std::max() returns the larger of two values, or the largest in a list. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns the larger of the two values (a reference), or the largest value of an initializer list. If values are equal, the first one is returned.
It returns the first argument.
Use std::max_element(v.begin(), v.end()). It returns an iterator.

Next: max_element()

Finds the largest element in a range.

max_element() 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.

19 people found this page helpful