std::min_element in C++

24 Jul, 2017
For calculating the smallest of all the elements in a given list, we have std::min, but what if we want to find the smallest not in the whole list, but in a sub-section of the list. To serve this purpose, we have std::min_element in C++.
std::min_element is defined inside the header file <algorithm> and it returns an iterator pointing to the element with the smallest value in the range [first, last).
Unlike std::min, which can be used in three ways, std::min_element can be used in two ways. The comparisons can be performed using either operator < (first version), or using a pre-defined function (second version). If more than one element satisfies the condition of being the smallest, the iterator returned points to the first of such elements.The two versions are defined as given below:
For comparing elements using “<":Syntax:template 
ForwardIterator min_element (ForwardIterator first, ForwardIterator last);

first: Forward iterator pointing to the beginning of the range.
last: Forward iterator pointing to the end of the range.

Return Value: It return a pointer to the smallest 
element in the range, and in case if there are more than one such element,
then it points to the first one.
It points to the last in case the range is empty.
// C++ program to demonstrate the use of std::min_element#include <iostream>#include <algorithm>using namespace std;int main(){    int v[] = { 9, 4, 7, 2, 5, 10, 11, 12, 1, 3, 6 };      // Finding the minimum value between the third and the    // fifth element      int* i1;    i1 = std::min_element(v + 2, v + 5);      cout << *i1 << "\n";    return 0;}Output:2
For comparison based on a pre-defined function:Syntax:template 
ForwardIterator min_element (ForwardIterator first, ForwardIterator last,
                             Compare comp);
Here, first and last are the same as previous case.
comp: Binary function that accepts two elements 
in the range as arguments, and returns a value convertible to bool.
The value returned indicates whether the element passed as first 
argument is considered less than the second.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.

Return Value: It return a pointer to the smallest element 
in the range, and in case if there are more than one such element,
then it points to the first one.
It points to the last in case the range is empty.
// C++ program to demonstrate the use of std::min_element#include <iostream>#include <algorithm>using namespace std;  // Defining the BinaryFunctionbool comp(int a, int b){    return (a < b);}  int main(){    int v[] = { 9, 4, 7, 2, 5, 10, 11, 12, 1, 3, 6 };      // Finding the minimum value between the third and the    // ninth element      int* i1;    i1 = std::min_element(v + 2, v + 9, comp);      cout << *i1 << "\n";    return 0;}Output:1

For comparing elements using “<":Syntax:template 
ForwardIterator min_element (ForwardIterator first, ForwardIterator last);

first: Forward iterator pointing to the beginning of the range.
last: Forward iterator pointing to the end of the range.

Return Value: It return a pointer to the smallest 
element in the range, and in case if there are more than one such element,
then it points to the first one.
It points to the last in case the range is empty.
// C++ program to demonstrate the use of std::min_element#include <iostream>#include <algorithm>using namespace std;int main(){    int v[] = { 9, 4, 7, 2, 5, 10, 11, 12, 1, 3, 6 };      // Finding the minimum value between the third and the    // fifth element      int* i1;    i1 = std::min_element(v + 2, v + 5);      cout << *i1 << "\n";    return 0;}Output:2

template 
ForwardIterator min_element (ForwardIterator first, ForwardIterator last);

first: Forward iterator pointing to the beginning of the range.
last: Forward iterator pointing to the end of the range.

Return Value: It return a pointer to the smallest 
element in the range, and in case if there are more than one such element,
then it points to the first one.
It points to the last in case the range is empty.

// C++ program to demonstrate the use of std::min_element#include <iostream>#include <algorithm>using namespace std;int main(){    int v[] = { 9, 4, 7, 2, 5, 10, 11, 12, 1, 3, 6 };      // Finding the minimum value between the third and the    // fifth element      int* i1;    i1 = std::min_element(v + 2, v + 5);      cout << *i1 << "\n";    return 0;}
Output:
2

For comparison based on a pre-defined function:Syntax:template 
ForwardIterator min_element (ForwardIterator first, ForwardIterator last,
                             Compare comp);
Here, first and last are the same as previous case.
comp: Binary function that accepts two elements 
in the range as arguments, and returns a value convertible to bool.
The value returned indicates whether the element passed as first 
argument is considered less than the second.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.

Return Value: It return a pointer to the smallest element 
in the range, and in case if there are more than one such element,
then it points to the first one.
It points to the last in case the range is empty.
// C++ program to demonstrate the use of std::min_element#include <iostream>#include <algorithm>using namespace std;  // Defining the BinaryFunctionbool comp(int a, int b){    return (a < b);}  int main(){    int v[] = { 9, 4, 7, 2, 5, 10, 11, 12, 1, 3, 6 };      // Finding the minimum value between the third and the    // ninth element      int* i1;    i1 = std::min_element(v + 2, v + 9, comp);      cout << *i1 << "\n";    return 0;}Output:1

Syntax:
template 
ForwardIterator min_element (ForwardIterator first, ForwardIterator last,
                             Compare comp);
Here, first and last are the same as previous case.
comp: Binary function that accepts two elements 
in the range as arguments, and returns a value convertible to bool.
The value returned indicates whether the element passed as first 
argument is considered less than the second.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.

Return Value: It return a pointer to the smallest element 
in the range, and in case if there are more than one such element,
then it points to the first one.
It points to the last in case the range is empty.

// C++ program to demonstrate the use of std::min_element#include <iostream>#include <algorithm>using namespace std;  // Defining the BinaryFunctionbool comp(int a, int b){    return (a < b);}  int main(){    int v[] = { 9, 4, 7, 2, 5, 10, 11, 12, 1, 3, 6 };      // Finding the minimum value between the third and the    // ninth element      int* i1;    i1 = std::min_element(v + 2, v + 9, comp);      cout << *i1 << "\n";    return 0;}
Output:
1

Related Articles:
std::max_element
std::max
std::min
std::equal
This article is contributed by Mrigendra Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
cpp-algorithm-library
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.