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++.
Example:
C++
// C++ program to illustrate the use of
// std::min_element()
#include <bits/stdc++.h>
using namespace std;
bool comp (int a, int b) {
return a < b;
}
int main()
{
vector<int> v = {2, 1, 17, 10};
int arr[4] = {33, 87, 1, 71};
int n = sizeof(arr) / sizeof(arr[0]);
// Min element in vector
cout << *min_element(v.begin(), v.end())
<< endl;
// Min element in array
cout << *max_element(v.begin(), v.end());
return 0;
}
std::min_element() Syntax
std::min_element(first, last, comp);
Parameters
- first: Iterator to the first element of the range.
- last: Iterator to the element just after the last element of the range.
- comp: Binary function, functor or lambda expression that compares two elements in the range. By default, it is set as < operator.
Return Value
- Returns an iterator to the smallest element in the range.
- When the range is empty, it returns iterator to the last.
More Examples of std::min_element()
We can find the minimum element of the given range using std::min_element().
Example 1: Find Minimum Element in Array
C++
// C++ program to illustrate the use of
// std::min_element() in array
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[] = {33, 87, 1, 71};
int n = sizeof(arr) / sizeof(arr[0]);
// Finding the minimum element in array
cout << *min_element(arr, arr + n);
return 0;
}
Time Complexity: O(n), where n is the number of elements in array.
Auxiliary Space: O(1)
Example 2: Finding Minimum Element in Deque with Multiple Minimums
When there are multiple minimum elements present in the range, std::min_element() returns the iterator to the first minimum element.
C++
// C++ program to illustrate the use of
// std::min_element() in deque
#include <bits/stdc++.h>
using namespace std;
int main() {
deque<int> d = {33, 1, 87, 1, 71, 1};
// Finding the minimum element in the deque
auto min = min_element(d.begin(), d.end());
// Finding the position
cout << "Index: " << distance(d.begin(), min);
return 0;
}
Time Complexity: O(n), where n is the number of elements in deque.
Auxiliary Space: O(1)
Example 3: Find Minimum Element in Vector of User Defined Data Type
We have to use a custom comparator to determine how to compare user-defined data types based on any of their properties.
C++
// C++ Program to find the minimum element in
// the vector of structure
#include <bits/stdc++.h>
using namespace std;
struct St {
string name;
int sno;
};
int main() {
// Create a vector of structure
vector<St> v = {{"Ashok", 11}, {"Deepak", 15},
{"Anmol", 23}, {"Vikas", 19}};
// Find the minimum element in the vector of structure
// based on the sno field
St min = *min_element(v.begin(), v.end(),
[](const St &i, const St &j) {
return i.sno < j.sno;
});
cout << min.name << " " << min.sno;
return 0;
}
Time Complexity: O(n), where n is the number of elements in the vector.
Auxiliary Space: O(1)
Working of std::min_element()
std::min_element() implements a linear search algorithm to find the smallest element in the range. It compares each element of the range one by one using the iterator/pointer provided to it as arguments. This is the reason why it gives O(n) linear time complexity.
std::min_element() is not specialized for sorted containers such as std::set, std::map, etc., and still compares all the elements of these containers to find the minimum element.
Similar Reads
std::nth_element in C++ std::nth_element() is an STL algorithm that rearranges the list in such a way such that the element at the nth position is the one which should be at that position if we sort the list. It does not sort the list, just that all the elements, which precede the nth element are not greater than it, and a
7 min read
max_element in C++ STL The std::max_element() in C++ is an STL algorithm that is used to find the maximum element in the given range. It is defined inside the <algorithm> header file. In this article, we will learn how to find the maximum element in the range using std::max_element() in C++.Example:C++// C++ program
4 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 in C++ 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
3 min read
std::minmax() and std::minmax_element() in C++ STL C++ defined functions to get smallest and largest elements among 2 or in a container using different functions. But there are also functions that are used to get both smallest and largest element using a single function, "minmax()" function achieves this task for us. This function is defined in "alg
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