Home C++ Algorithms Functions min_element() C++ std::min_element() Function 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.
std::min_element(v.begin(), v.end());
std::min_element(v.begin(), v.end(), comp); Call What 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 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 Parameter Description first, last Required. The range to search. comp Optional. A function that returns true when its first argument is smaller.
Return Value Return Value Description Iterator Points at the smallest element (the first one if there is a tie). lastReturned when the range is empty.
C++ Examples 1. Smallest value and index 2. Shortest word 3. First of equal values Example 1 — Smallest value and index Find the smallest number and where it is.
#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 min_element() checks every value once.1 is the smallest and sits at index 3. *it reads the value and it - v.begin() gives the index.Example 2 — Shortest word Use a comparison on strings.
#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 The lambda compares word lengths, not alphabet order. fig has 3 letters, the fewest.The iterator points at fig. Example 3 — First of equal values Ties return the earliest element.
#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;
} First smallest at index 1 How It Works There are two 1s, at index 1 and 2. min_element() keeps the first one it saw.So the result is index 1. Frequently Asked Questions What does std::min_element() do? In C++, std::min_element() finds the smallest element in a range. Include <algorithm> to use it.
Which header and C++ version do I need for std::min_element()? Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
What does std::min_element() return? It returns an iterator to the smallest element, or the last iterator if the range is empty.
What is the difference between min() and min_element()? min() compares values you give it. min_element() searches a whole range and returns an iterator.
Which element is returned on a tie? 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 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.
17 people found this page helpful
Helpful Share Copy link Suggestion