C++ Algorithm max function can be used in following 3 ways:
- It compares the two values passed in its arguments and returns the larger between them . If both are equal, then it returns the first one.
- It also compares the two values using a binary function which is defined by the user, and then passed as an argument in std::max.
- It is also used to find the largest element in a given list , and it returns the first one if there are more than one are largest in the list.
Elements are compared using the < operator in the initial version or through the provided binary comparison function comp in the alternative version.
Syntax
default (1) template <class T> const T& max (const T& a, const T& b); //until C++ 11
custom (2) template <class T, class Compare>
const T& max (const T& a, const T& b, Compare comp); //until C++ 11
default (1) template <class T> const T& max (const T& a, const T& b); //until C++ 14
custom (2) template <class T, class Compare>
const T& max (const T& a, const T& b, Compare comp); //until C++ 14
initializer list (3) template <class T> T max (initializer_list<T> il);
template <class T, class Compare>
T max (initializer_list<T> il, Compare comp); //until C++ 14
default (1) template <class T> constexpr const T& max (const T& a, const T& b); //since C++ 14
//since C++ 14
custom (2) template <class T, class Compare>
constexp const T& max(const T& a, const T& b, Compare comp);
// since C++ 14
initializer list (3) template <class T> constexpr T max (initializer_list<T> il);
template <class T, class Compare>
constexpr T max (initializer_list<T> il, Compare comp);
//since C++ 14
Parameter
a : First value to compare.
b : Second value to compare.
A custom binary predicate function named comp is defined by the user. This function takes two arguments and evaluates to true if the two arguments are in the correct order; otherwise, it evaluates to false. The comp function adheres to the strict weak ordering principle for sorting elements.
An initializer_list containing the values for comparison.
Return value
It retrieves the highest value between a and b. In case both values are the same, it will return a.
Returns the highest value in the list "il". In case multiple values are equal to the maximum, it returns the leftmost value among them.
Complexity
The complexity increases linearly as the number of elements being compared decreases by one.
Exceptions
This function will raise an exception if any error occurs during a comparison operation.
Note: The invalid parameters cause an undefined behavior.
Example 1
Let's examine a basic example to illustrate the functionality of the max function:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "larger of 1 and 9999: " << std::max(1, 9999) << '\n'
<< "larger of 'a', and 'b': " << max('a', 'b') << '\n'
<< "longest of \"foo\", \"bar\", and \"hello\": " <<
max( { "foo", "bar", "hello" },
[](const string& s1, const string& s2) {
return s1.size() < s2.size();
}) << '\n';
return 0;
}
Output:
larger of 1 and 9999: 9999
larger of 'a', and 'b': b
longest of "foo", "bar", and "hello": hello
Example 2
Let's explore another straightforward example to showcase the application of max with the default parameters:
#include <iostream> // std::cout
#include <algorithm> // std::max
using namespace std;
int main () {
cout << "max(1,2)==" << max(1,2) << '\n';
cout << "max(2,1)==" << max(2,1) << '\n';
cout << "max('a','z')==" << max('a','z') << '\n';
cout << "max(3.14,2.73)==" << max(3.14,2.73) << '\n';
return 0;
}
Output:
max(1,2)==2
max(2,1)==2
max('a','z')==z
max(3.14,2.73)==3.14
Example 3
Let's explore another basic example to illustrate the utilization of the max function with a custom comparison function:
#include<iostream>
#include<algorithm>
using namespace std;
// Defining the binary function
bool comp(int a, int b)
{
return (a < b);
}
int main()
{
int a = 7;
int b = 28;
cout << max(a,b,comp) << "\n";
// Returns the first one if both the numbers
// are same
cout << max(7,7,comp);
return 0;
}
Output:
Example 4
Let's explore a basic example to determine the highest value in a given list:
#include<iostream>
#include<algorithm>
using namespace std;
// Defining the binary function
bool comp(int a, int b)
{
return (a < b);
}
int main()
{
// Finding the largest of all the numbers
cout << "Maximum element is: "<< max({1, 2, 3, 4, 5, 10, -1, 7},comp) << "\n";
return 0;
}
Output:
Maximum element is: 10