C++

Max Function In C++ – Examples

As you know C++ language is an extension of C language that facilitates a developer with many built-in (already defined code in form of Libraries) functionalities. Their many functions in C++, one of them is max() which returns the maximum value of an array or any other data structure. This write-up provides you with a detailed overview of the max() function in C++.

What is the max() Function In C++

The max() function is a function in C++ that gives the greatest value from all the function parameters that are passed; hence there are many ways in which we will apply the max () function.

C++ max() – Simple Algorithm

  • Take the first value of the function argument
  • Take the second value of the function argument
  • Returns the bigger value if both values=equal returns first-value

Let’s discuss the max() function in C++ language in different ways of initialization one after the other.

1. Syntax of max() – Passing Two Variables

max(num1, num2)

It will return a bigger value among m and n.

2. Syntax of max() – With the Use of the List

max(list, Compare cmp);

Gives the maximum number of list elements.

3. Syntax of max() – With the Use of the Binary Function

max(num1, num2, Compare cmp);

The Largest value will be returned through the max() function.

Restriction and Parameters of max() Function

  • num1= Value one which needs to be compared
  • num2= Value two which also needs to be compared
  • Compare cmp= It will give the value in the form of true and false and it is not mandatory to use.

Let’s see through different examples of max() in C++ code.

Example 1: max() Function with Two Numbers

#include <iostream>
#include <algorithm>

using namespace std;

int main () {

    int r = max (50, 75);

    cout << "maximum number is....: " << r << endl;

}

The above program takes the variable as maximum function arguments and prints it on the screen as the following output.

Output

Example 2: max() Function with Binary Function

#include<iostream>
using namespace std;

bool cmp (int num1, int num2) {

    return (num1 < num2);

}

int main () {

    int x = 3;

    int y = 6;

      cout << "output with greater value:...."<<std::max(x, y ,cmp) << "\n";

    return 0;

}

In the above, cmp is a binary function and in the main, we take two variables and print the maximum value.

Output

Example 3: max() Function with List

#include<iostream>

#include<algorithm>

using namespace std;

 

bool compare (int num1, int num2)

{

return (num1 < num2);

}

int main ()

{

int num3 = max({1,2,4,8,9}, compare);

cout << num3 << "\n";

return 0;

}

After initializing header files in the above code, there is a main function of compare which compares list elements and returns the large one in the f variable.

Output

Example 3: max() Function with Characters

#include <iostream>
using namespace std;

int main ()

{

cout << "Greater Element between r and s is: " << max ('r', 's');

return 0;

}

This code will print the larger element using the std library of max().

Output


Note: The max() is restricted to having the same data type of variables that are passed to the parameters of the function. It generates an error if the variables are not having the same data type in function parameters.

Conclusion

The max() in C++ will be very helpful in finding the greatest element between the various elements. It can be used in all kinds of data types and in various data structures like arrays and structures. The max function is very easy to implement in C++. The above article explained different methods of using max() in C++.

About the author

Kaynat Asif

My passion to research new technologies has brought me here to write for the LinuxHint. My major focus is to write in C, C++, and other Computer Science related fields. My aim is to share my knowledge with other people.