Introduction to C++ Max Function
In C++, max is a function that is used to get the largest among the elements. In order to achieve that, there are different ways such as comparing two elements passed as arguments and returning largest among them, comparing two elements with the help of a binary function and passing it as an argument in std::max(), and at last, finding the largest element in the list. In this article, we will look into more about these ways using different examples and explanations using syntaxes.
Syntax:
As already mentioned, the max function can be used in three ways. Let us see each syntax in detail.
- Syntax of max when comparison of elements is done using “<“:
template constexpr const T& max ( const T& num1 , const T& num2 ) ;
Here, num1 and num2 are the two numbers that has to be compared to finding the largest value.
Return value: Largest among num1 and num2.
- Syntax of max when comparison of elements is done using predefined functions
template constexpr const T& max ( const T& num1 , const T& num2. Compare cmp ) ;
Here, num1 and num2 are the two numbers that has to be compared to finding the largest value.
Cmp is the binary function that takes two values as arguments and returns a Boolean convertible value. The return value of this binary function indicates whether the value passed as argument one is less than argument two. Moreover, the function does not alter any arguments and also, this function can be a function object or function pointer.
Return value: Largest among num1 and num2.
- Syntax of max for finding a maximum element in the list
template constexpr T max (initializer_list li, Compare cmp);
In this syntax, cmp is optional. That is, it can be skipped.
li is the object of the initializer_list.
Return value: Largest among all the values.
Examples of C++ Max
The following are some sample programs on the max function in C++.
Example #1
Print the Largest Element using std::max() function with Two Integer Parameters
Code:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
//main method
int main()
{
//print the largest element using std::max
cout << "Largest of the elements 345 and 6748: " << std::max(345, 6748);
return 0;
}
Output:
In this program, all the necessary libraries are imported first. Then, two number 345 and 6748 are passed as parameters in std::max in order to find the largest element. On executing the code, the maximum element of 6748 gets printed.
Example #2
Print the Largest Element using std::max() function with Two Char Parameters
Code:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
//main method
int main()
{
//print the largest element using std::max
cout << "Largest of the elements x and y: " << max('x', 'y');
return 0; }
Output:
In this program also, all the necessary libraries are imported first. Unlike the above program, two characters x and y are compared in order to find the largest element. On executing the code, the maximum element y gets printed.
Example #3
Print the Largest Element in a List of Strings using std::max() Function
Code:
//import the necessary libraries
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
//main method
int main()
{
//print the largest element using std::max
cout << "Largest of the elements in the given list: " << max( { "happy" , "happymoment" , "happymomentsarewaiting" } ,
[]( const string& str1 , const string& str2 ) { return str1.size() < str2.size() ;
} ) ;
return 0;
}
Output:
First, all the necessary libraries are imported and a list of strings is compared to find the largest element. On executing the code, the maximum element happymomentsarewaiting gets printed
Example #4
Print the Largest Element using a Binary Function
Code:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
//function to find the largest element
bool cmp(int num, int num2)
{
return (num < num2);
}
//main method
int main()
{
int a = 45 ;
int b = 345 ;
//call the function cmp using the max() function
cout << "largest element among the number 45 and 345 is :" << std::max( a , b , cmp ) << "\n" ;
return 0 ;
}
Output:
In this program, two integers a and b are declared. A binary function cmp() is defined which compares two elements passed as the parameters. On executing the code, largest element 345 gets printed.
Example #5
Print the Largest Element in a List
Code:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
//function to find the largest element
bool cmp(int num, int num2)
{
return (num < num2);
}
//main method
int main()
{
//call the function cmp using the max() function
cout << "largest element in the list { 971 , 268 , 573 , 423 , 544 , 310 , -13 , 74 } is: "<< max({ 971 , 268 , 573 , 423 , 544 , 310 , -13 , 74 }, cmp) << "\n" ;
return 0 ;
}
Output:
In this program, the largest among a list of numbers gets printed on executing the code.
Example #6
Print the Largest Element if the Same Number is Compared
Code:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
//function to find the largest element
bool cmp(int num, int num2)
{
return (num < num2);
}
//main method
int main()
{
int a = 45 ;
int b = 45 ;
//call the function cmp using the max() function
cout << "largest element among the number 45 and 45 is :" << std::max( a , b , cmp ) << "\n" ;
return 0 ;
}
Output:
In this program, the same elements are compared and as a result, that number itself gets printed.
Conclusion
Max is a function in C++ that is used to obtain the largest element among the given elements. In this article, different ways of using the max function are explained in detail.
Recommended Articles
This is a guide to C++ Max. Here we discuss the Definition of C++ Max Function and examples with code implementation respectively. You may also have a look at the following articles to learn more –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses