C++ is a programming language that helps in building high-performance applications. It was developed by Bjarne Stroustrup. In C++, many valuable functions help the users while coding. Now, there are multiple functions like std::max(), std::sort(), std::toupper(), std::find(), etc.
In this article, we will study the std::min in C++.
What is std::min in C++?
std::min in C++ finds the smallest number from the given parameters passed into it. It is defined in the <algorithm> header file. It is used in the following manners:-
It compares two numbers passed into it and returns the smallest one. If both of them are equal, it returns the first one.
It is capable of comparing the two functions using a user-defined binary function. This function is also passed as a parameter in the function.
It can also return the smallest element in the passed list. When there is more than one element as the answer, it returns the first element.
Versions of std::min in C++
Now, let us see the three versions of std::min in C++.
template <class T> const T& min(const T&a, const T&b); It returns the smallest element of the two elements passed into it. Let us understand this by the code below:
EXAMPLE
C++
C++
EXAMPLE #include<iostream> #include<algorithm> using namespace std;
We can see that the minimum of the two elements in each case is displayed.
template <class T, class Compare> const T& min(const T&a, const T&b, Compare comp); In this, we create a comparison function and then define the rules. Let us understand this with the code below:
EXAMPLE
C++
C++
#include<iostream> #include<algorithm> using namespace std; bool comparison(int a, int b){ return a<b; } int main(){ cout<<std::min(8, 9, comparison); return 0; }
You can also try this code with Online C++ Compiler
We have created a function named comparison for returning the minimum value of both elements.
template constexpr T min(initializer_list il, Compare comp); In this version, std::min in C++ returns the smallest element in the list. Let us understand this by the code below.
EXAMPLE
C++
C++
#include<iostream> #include<algorithm> using namespace std;
bool cmp(int a, int b){ return a<b; } int main(){ cout<<std::min({1,4,7,0,-9}, cmp); return 0; }
You can also try this code with Online C++ Compiler
C++ is a programming language that helps in building high-performance applications. It was developed by Bjarne Stroustrup. It was first released in 1985. It is a high-level language and has a general purpose too.
What is std::min in C++?
std::min in C++ finds the smallest number from the given parameters passed into it. It is defined in the <algorithm> header file. It can find the smallest element from the two passed parameters or a list of elements.
Can std::min in C++ be applied to more than two elements?
Yes, std::min in C++ can be applied to more than two elements. It can be applied to a list of elements and will return the smallest element. Also, we can write a comparison function for it too.
Conclusion
C++ is a high-level language for general purposes and for creating applications of high standards. In this article, we studied std::min in C++. We looked at its definition, its different versions, and its implementations.
If you wish to learn more about this concept, read the following articles: