Find the Maximum and Minimum in C++
Whether you’re finding the top score, the cheapest item, or the biggest number in a list, C++ gives you clean, built-in tools for the job. Let’s go from comparing two numbers up to scanning an entire vector.
The Larger of Two Numbers: std::max and std::min
For just two values, the <algorithm> header has exactly what you need. std::max returns the larger; std::min returns the smaller:
#include <iostream>
#include <algorithm>
int main() {
int a = 12, b = 7;
std::cout << "Max: " << std::max(a, b) << "\n"; // 12
std::cout << "Min: " << std::min(a, b) << "\n"; // 7
return 0;
}
These read like plain English and work with any comparable type — int, double, even std::string. No if statement required.
The Max or Min of Several Values
Since C++11, you can pass a whole list in braces to std::max and std::min, and they’ll pick the winner:
#include <iostream>
#include <algorithm>
int main() {
int biggest = std::max({3, 9, 2, 15, 8}); // 15
int smallest = std::min({3, 9, 2, 15, 8}); // 2
std::cout << biggest << " " << smallest << "\n";
return 0;
}
This is perfect when you have a fixed handful of values and just want the extreme one.
The Largest Element in an Array or Vector
When your numbers live in a container, reach for std::max_element (and its twin std::min_element). You give it a range, and it finds the biggest element for you:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> scores = {72, 95, 60, 88, 91};
auto it = std::max_element(scores.begin(), scores.end());
std::cout << "Highest score: " << *it << "\n"; // 95
return 0;
}
Important: It Returns an Iterator, Not a Value
Here’s the detail beginners miss. std::max_element doesn’t hand back the number — it hands back an iterator that points at the number. That’s why we wrote *it with a star: the * reads the value the iterator points to.
Forget the * and you’ll get a confusing compiler error, because you’d be trying to print the pointer-like iterator itself. If you only need the value in one line, dereference right away:
int highest = *std::max_element(scores.begin(), scores.end());
Getting the Position, Too
Because you get an iterator, you can also find out where the maximum lives using std::distance:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> scores = {72, 95, 60, 88, 91};
auto it = std::max_element(scores.begin(), scores.end());
int value = *it;
int index = std::distance(scores.begin(), it);
std::cout << "Value " << value << " is at index " << index << "\n"; // index 1
return 0;
}
That’s a real advantage over a hand-written loop — you get both the value and its position for free.
Doing It By Hand (For Understanding)
It’s worth writing the loop version once, so the standard tools don’t feel like magic. Start by assuming the first element is the biggest, then update whenever you find something larger:
#include <iostream>
#include <vector>
int main() {
std::vector<int> scores = {72, 95, 60, 88, 91};
int largest = scores[0];
for (int v : scores) {
if (v > largest) largest = v;
}
std::cout << "Largest: " << largest << "\n"; // 95
return 0;
}
This is exactly what std::max_element does internally. In real code, prefer the standard version — it’s tested, clear, and less error-prone.
Quick Reference
| Task | Tool |
|---|---|
| larger of two values | std::max(a, b) |
| smaller of two values | std::min(a, b) |
| max/min of a fixed list | std::max({...}) |
| largest in a container | *std::max_element(b, e) |
| smallest in a container | *std::min_element(b, e) |
| position of the max | std::distance(b, it) |
Related Articles
- How to Find an Element in a Vector in C++ — searching with std::find
- C++ Vector Tutorial — the container these tools scan
- Sum of Array Elements in C++ — the sibling task to finding extremes
- C++ Iterators Explained — why max_element returns an iterator
- C++ Conditionals Tutorial — the if behind the by-hand version
Take Your C++ Further
If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.