C++ std::max() Function

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.
std::max(a, b);
std::max({4, 9, 2, 7});
| Call | What 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
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
| Parameter | Description |
|---|
| a, b | Required (two-value form). The values to compare. |
| list | A braced list such as {4, 9, 2}. It must not be empty. |
| comp | Optional. A comparison function that returns true when its first argument is smaller than the second. |
Return Value
| Return Value | Description |
|---|
| Reference | The two-value form returns a reference to the larger argument. If they are equal, it returns the first. |
| Value | The list form returns a copy of the largest value. |
C++ Examples
Example 1 — Two numbers
Pick the larger of two numbers.
#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
max(a, b) compares the two values.- 8 is greater than 3, so it is returned.
- The result is copied into
bigger.
Example 2 — Largest in a list
Pass many values in braces.
#include <algorithm>
#include <iostream>
int main() {
int largest = std::max({4, 9, 2, 7});
std::cout << "Largest: " << largest << '\n';
return 0;
}
How It Works
- The braces form an initializer list.
max() checks each value and keeps the biggest.- 9 is the largest of the four.
Example 3 — Longer word
Choose the longer of two strings.
#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
- The lambda says
x is "smaller" when it has fewer letters. cat has 3 letters and horse has 5.- 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.
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.
19 people found this page helpful