C++ std::min_element() Function

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

Definition

In C++, std::min_element() scans a range and returns an iterator to the smallest element. If several elements are equally small, it returns the first one.

Use it when you need the position as well as the value.

Syntax

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

C++
std::min_element(v.begin(), v.end());
std::min_element(v.begin(), v.end(), comp);
CallWhat it does
std::min_element(v.begin(), v.end());Iterator to the smallest element.
std::min_element(v.begin(), v.end(), comp);Use your own rule for "smaller".
Library declarations
C++
template<class ForwardIt>
ForwardIt min_element(ForwardIt first, ForwardIt last);

template<class ForwardIt, class Compare>
ForwardIt min_element(ForwardIt first, ForwardIt last, Compare comp);

Parameters

ParameterDescription
first, lastRequired. The range to search.
compOptional. A function that returns true when its first argument is smaller.

Return Value

Return ValueDescription
IteratorPoints at the smallest element (the first one if there is a tie).
lastReturned when the range is empty.

C++ Examples

Example 1 — Smallest value and index

Find the smallest number and where it is.

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

int main() {
    std::vector<int> v = {7, 3, 9, 1, 5};

    auto it = std::min_element(v.begin(), v.end());

    std::cout << "Smallest: " << *it << " at index " << (it - v.begin()) << '\n';
    return 0;
}

How It Works

  1. min_element() checks every value once.
  2. 1 is the smallest and sits at index 3.
  3. *it reads the value and it - v.begin() gives the index.

Example 2 — Shortest word

Use a comparison on strings.

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

int main() {
    std::vector<std::string> words = {"banana", "fig", "apple"};

    auto it = std::min_element(words.begin(), words.end(),
                               [](const std::string& a, const std::string& b) {
                                   return a.size() < b.size();
                               });

    std::cout << "Shortest: " << *it << '\n';
    return 0;
}

How It Works

  1. The lambda compares word lengths, not alphabet order.
  2. fig has 3 letters, the fewest.
  3. The iterator points at fig.

Example 3 — First of equal values

Ties return the earliest element.

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

int main() {
    std::vector<int> v = {2, 1, 1, 3};

    auto it = std::min_element(v.begin(), v.end());

    std::cout << "First smallest at index " << (it - v.begin()) << '\n';
    return 0;
}

How It Works

  1. There are two 1s, at index 1 and 2.
  2. min_element() keeps the first one it saw.
  3. So the result is index 1.

Frequently Asked Questions

In C++, std::min_element() finds the smallest element in a range. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns an iterator to the smallest element, or the last iterator if the range is empty.
min() compares values you give it. min_element() searches a whole range and returns an iterator.
The first smallest element.

Next: minmax()

Returns the smaller and the larger of two values (or of a list) at once.

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

17 people found this page helpful