C++ std::minmax_element() Function

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

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.

C++
auto p = std::minmax_element(v.begin(), v.end());
*p.first, *p.second
CallWhat it does
auto p = std::minmax_element(v.begin(), v.end());Get both iterators with one call.
*p.first, *p.secondThe smallest and the largest value.
Library declarations
C++
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

ParameterDescription
first, lastRequired. The range to search.
compOptional. A comparison function.

Return Value

Return ValueDescription
std::pairfirst points at the smallest element and second points at the largest.
Empty rangeBoth iterators equal last.

C++ Examples

Example 1 — Smallest and largest

Get both values from one call.

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

  1. The vector is scanned only once.
  2. p.first points at 1, the smallest value.
  3. p.second points at 9, the largest.

Example 2 — Ties

See which duplicate each iterator picks.

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

How It Works

  1. The value 1 appears at index 2 and 4. The first one is chosen.
  2. The value 9 appears at index 1 and 3. The last one is chosen.
  3. That gives indexes 2 and 3.

Example 3 — Temperatures

Use the pair on decimal numbers.

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

How It Works

  1. Decimal numbers work like integers here.
  2. std::cout prints 19.0 as 19 because trailing zeros are dropped.
  3. 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.

Next: mismatch()

Finds the first position where two ranges differ.

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

23 people found this page helpful