Table of Contents
Introduction
Hey there! Today in this tutorial we are going to understand the working as well as the use of the std::max() function in C++.
The std::max() function
The std::max()
function in C++ is a pre-defined function used to find the maximum of two constant values(int, float, char, string, etc,). It is defined in the <algorithm>
header file. The max()
function declaration in C++ is given below.
std::max(const T& a, const T& b, Compare comp);
Here, a
and b
are the two values. And comp
(optional) is the comparing function on the basis of which the comparison is to be done.
The max()
function can also be used for finding the maximum value from an initializer list
with an optional comp
function. The syntax is given below. Note: this is only valid for C++11 and above versions.
std::max( std::initializer_list<T> ilist, Compare comp );
Finding the Maximum values with C++ max() function
So now that we have familiarized ourselves with the max()
function, let us get into some examples.
#include<iostream> #include<algorithm> using namespace std; int main() { int a=10, b=20, max; // max stores the maximum integer max = std::max(a, b); cout<<"The max number is:"<<max; return 0; }
Output:
The max number is: 20
Here, the max()
function returns the maximum value among the passed a
and b
variables i.e. 20 as expected.
As we mentioned earlier, the function also works for initializer lists
. Have a look at the example given below.
#include<iostream> #include<algorithm> using namespace std; int main() { // with initialiser list cout<<max({ 12, 13, 45, 78, 32}); return 0; }
Output:
78
We get our desired maximum element 78.
C++ max() function with comp function
The comp
(optional) parameter has to be a function that defines the comparison for the values to find the maximum one. This function further needs to return a bool
value on the basis of the provided condition.
So, we can either define a lambda function
or a user-defined bool
function to accomplish this task.
1. comp as lambda function
A lambda function is a one-line user-defined function introduced from C++11 and later versions.
Look at the example given below carefully.
#include<iostream> #include<algorithm> using namespace std; int main() { int a=5, b=7; // with lambda function cout<<max(a, b, [](int a, int b) { return (a < b);}); return 0; }
Output:
7
Here, we have defined a lambda
function that takes two arguments a
and b
and checks whether the first one is smaller than the other. It returns a boolean value(True
or False
).
2. comp as bool function
Similarly, we can also define a bool
function to compare two arguments.
#include<iostream> #include<algorithm> using namespace std; bool Myfunc(int a, int b) { return (a<b); } int main() { int a=118, b=52; cout<<max(a, b, Myfunc); return 0; }
Output:
118
As you can see, we get our desired output(118)
Conclusion
That’s it for today. Hope you had a clear understanding of the std::max() function in C++.
For any related questions, feel free to use the comments below.
References
- std::max() – CPP reference,
- Lambda functions in C++ – Microsoft Documentation,
- C++ Tutorial.