Updated July 1, 2023
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 the 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 ) ;
Num1 and num2 are the two numbers that must be compared to find 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 ) ;
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 an argument, one is less than argument two. Moreover, the function does not alter any arguments; this function can also 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:
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:
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:
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:
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:
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:
Conclusion
The max function in C++ is a powerful tool that allows programmers to find the largest element among a given set of elements. This function plays a crucial role in various programming scenarios.
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 –