The std::min() is used to find the minimum element among the given elements. It is the built-in function of C++ STL defined inside <algorithm> header file.
Let's take the simplest example to demonstrate how the min() function works:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
cout << min(3,7);
return 0;
}
min() Function Signature
C++
template< class T, class Compare >
const T& min (const T& a, const T& b, Compare comp);
template< class T, class Compare >
T min (std::initializer_list<T> ilist, Compare comp);
The std::min() function can be used in three different ways to:
Find Minimum Among Two Elements
We can use std::min() function to find the smaller elements between the two elements. It uses < operator for comparison.
C++
Here,
- a: First value
- b: Second value
This function returns the smaller of the two values. If both are equal, returns the first value.
Example
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
int a = 8, b = 91;
// Finding minimum among a and b
cout << min(a, b);
return 0;
}
Time complexity: O(1)
Auxiliary Space: O(1)
Find the Minimum Among the Multiple Values
The std::min() function can also find the minimum value between more than two values. To achieve this, we have to pass the values in an initializer list, enclosed in {}
and separated by commas. It uses < operator with to compare all the values pairwise.
C++
Here,
- v1, v2, v3...: List of values.
This function returns the smallest value among the given lists. If all are equal, return the first value.
Example
C++
#include<bits/stdc++.h>
using namespace std;
int main() {
// Finding the smallest of
// all the numbers
cout << min({1, 2, 3, 4, 5,
10, -1, 7});
return 0;
}
Find Minimum Using Custom Comparator
The std::min() function also supports the use of custom comparator function to change the way of comparison. It is by default set to find the minimum element but we can also change it to perform any other desired comparison. It is especially useful to compare values of user defined data type.
C++
std::min (a, b, comp)
std::min ({v1, v2, v3...}, comp)
where, comp is the comparator function, lambda expression or even functors. This comparator function should follow these rules:
- Return value should be of bool or any bool convertible type.
- Should take two arguments.
- Should not modify the arguments.
It returns true if a is smaller than b. False otherwise.
Example
C++
#include <bits/stdc++.h>
using namespace std;
class St {
public:
int sno;
string name;
St(int val, string s): sno(val), name(s) {}
};
// Comparator that works
// for data type A
bool comp(const St& a, const St& b) {
return a.sno < b.sno;
}
int main() {
St a(8, "Divesh");
St b(91, "Rohan");
// std::min() with custom comparator
auto smaller = min(a, b, comp);
cout << smaller.sno << " "
<< smaller.name;
return 0;
}
Similar Reads
std::distance in C++ The std::distance() in C++ STL is a built-in function used to calculate the number of elements between two iterators. It is defined inside <iterator> header file. In this article, we will learn about the std::distance function in C++ with examples.Example:C++// C++ Program to illustrate the us
5 min read
std::find_end in C++ std::find_end is used to find the last occurrence of a sub-sequence inside a container. It searches the range [first1,last1) for the last occurrence of the sequence defined by [first2,last2), and returns an iterator to its first element, or last1 if no occurrences are found. It is similar to std::se
6 min read
std::min_element in C++ The std::min_element() in C++ is an STL algorithm that is used to find the minimum element in a given range. This range can be array, vector, list or any other container. It is defined inside the <algorithm> header file. In this article, we will learn about the std::min_element() in C++.Exampl
4 min read
is_sorted() in C++ STL In C++, is_sorted() is a built-in function used to check whether the element of the given range is sorted or not in ascending order. In this article, we will learn about is_sorted() function in C++.Letâs take a quick look at a simple example that illustrates is_sorted() method:C++#include <bits/s
4 min read
Min Heap in C++ A min-heap is a complete binary tree in which the value of each node is less than the value of its left child and right child. This property is true for every node in the tree. In this article, we will learn how we can implement the min heap data structure in C++. Implementation of Min Heap in C++A
8 min read