C++ std::minmax_element() Function

Definition
In C++, std::minmax_element() scans a range once and returns a pair of iterators: one to the smallest element and one to the largest.
It is faster than calling min_element() and max_element() separately. On ties it returns the first smallest and the last largest.
Syntax
Add #include <algorithm>, then call std::minmax_element(). Start with these two lines.
auto p = std::minmax_element(v.begin(), v.end());
*p.first, *p.second
| Call | What it does |
|---|
auto p = std::minmax_element(v.begin(), v.end()); | Get both iterators with one call. |
*p.first, *p.second | The smallest and the largest value. |
Library declarations
template<class ForwardIt>
std::pair<ForwardIt, ForwardIt>
minmax_element(ForwardIt first, ForwardIt last);
template<class ForwardIt, class Compare>
std::pair<ForwardIt, ForwardIt>
minmax_element(ForwardIt first, ForwardIt last, Compare comp);
Parameters
| Parameter | Description |
|---|
| first, last | Required. The range to search. |
| comp | Optional. A comparison function. |
Return Value
| Return Value | Description |
|---|
std::pair | first points at the smallest element and second points at the largest. |
| Empty range | Both iterators equal last. |
C++ Examples
Example 1 — Smallest and largest
Get both values from one call.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {7, 3, 9, 1, 5};
auto p = std::minmax_element(v.begin(), v.end());
std::cout << "Smallest: " << *p.first << '\n';
std::cout << "Largest: " << *p.second << '\n';
return 0;
}
How It Works
- The vector is scanned only once.
p.first points at 1, the smallest value.p.second points at 9, the largest.
Example 2 — Ties
See which duplicate each iterator picks.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {3, 9, 1, 9, 1};
auto p = std::minmax_element(v.begin(), v.end());
std::cout << "Smallest at index " << (p.first - v.begin())
<< ", largest at index " << (p.second - v.begin()) << '\n';
return 0;
}
Smallest at index 2, largest at index 3
How It Works
- The value 1 appears at index 2 and 4. The first one is chosen.
- The value 9 appears at index 1 and 3. The last one is chosen.
- That gives indexes 2 and 3.
Example 3 — Temperatures
Use the pair on decimal numbers.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<double> temps = {21.5, 19.0, 25.5, 23.0};
auto p = std::minmax_element(temps.begin(), temps.end());
std::cout << "Lowest: " << *p.first << '\n';
std::cout << "Highest: " << *p.second << '\n';
std::cout << "Range: " << (*p.second - *p.first) << '\n';
return 0;
}
Lowest: 19
Highest: 25.5
Range: 6.5
How It Works
- Decimal numbers work like integers here.
std::cout prints 19.0 as 19 because trailing zeros are dropped.- The range is the highest value minus the lowest value.
Frequently Asked Questions
In C++, std::minmax_element() finds the smallest and the largest elements of a range in one pass. Include <algorithm> to use it.
Include <algorithm>. It needs C++11 or newer, so compile with -std=c++11 or later.
It returns a pair of iterators: the first points to the smallest element and the second points to the largest element.
It reads the range only once, so it does fewer comparisons.
The first smallest and the last 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.
23 people found this page helpful