std::max in C++ Last Updated : 11 Jan, 2025 Comments Improve Suggest changes Like Article Like Report The std::max() is the built-in function in C++ used for finding the maximum among two or more elements passed to it as arguments. It is defined inside <algorithm> header file. In this article, we will learn how to use std::max() function in C++.The std::max() can be used in the following ways:Table of ContentFind Maximum Among Two ValuesFind the Maximum Among the Multiple ValuesFind Maximum Among Two ValuesWe can use std::max() function to find the larger among two values. We can also compare the two values using different parameters by defining the custom comparison function.Syntaxmax(a , b, comp);Parametersa: First value to be compared.b: Second value to be compared.comp: Binary function that accepts two values and returns a value convertible to bool. It is optional and by default set to return true if the first element is larger than second.Return ValueReturns the larger of the two values.If both are equal, returns the first value.The std::max function is essential for finding the maximum value between two values. Example: Finding Maximum Among Two Numbers using std::max() C++ // C++ program to find the larger number among // two numbers using std::max() #include <bits/stdc++.h> using namespace std; // Custom compare function bool comp(int a, int b) { return a > b; } int main() { int a = 8, b = 91; // Finding maximum among two numbers cout << max(a, b) << endl; // Again doing the same but with custom comparator cout << max(a, b, comp) << endl; return 0; } Output91 8 Time Complexity: O(1)Auxiliary Space: O(1)Find the Maximum Among the Multiple ValuesThe std::max() function can also find the maximum value between more than two values. We have to pass the values enclosed in { } braces and separated by a comma (initializer list).Syntaxmax ({v1, v2, v3...}, comp);Parameters:v1, v2, v3...: List of values.comp: Optional comparator function to change the way of comparison.Return ValueReturns the largest value among the given lists.If all are equal, return the first value.Example: Finding Largest Value Among the List of Integers C++ // C++ program to demonstrate how to find the // largest number among the list of numbers // using std::max() #include<bits/stdc++.h> using namespace std; int main() { // Finding the largest of all the numbers cout << max({1, 2, 3, 4, 5, 10, -1, 7}) << endl; return 0; } Output10 Time Complexity: O(n), where n is the number of elements. Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article std::max in C++ M Mrigendra Singh Improve Article Tags : C++ STL cpp-algorithm-library Practice Tags : CPPSTL Similar Reads std::min in C++ The std::min() is used to find the minimum element among the given elements. It is the built-in function of C++ STL defined inside <algorithm> header file.Let's take the simplest example to demonstrate how the min() function works:C++#include <bits/stdc++.h> using namespace std; int main 3 min read max_element in C++ STL The std::max_element() in C++ is an STL algorithm that is used to find the maximum element in the given range. It is defined inside the <algorithm> header file. In this article, we will learn how to find the maximum element in the range using std::max_element() in C++.Example:C++// C++ program 4 min read map max_size() in C++ STL The map::max_size() is a built-in function in C++ STL which returns the maximum number of elements a map container can hold. Syntax: map_name.max_size() Parameters: This function does not accept any parameters. Return Value: This function returns the maximum number of elements a map container can ho 1 min read Max Heap in C++ A max heap is defined as a complete binary tree where every node's value is at least as large as the values of its children. This makes it useful for implementing priority queues, where the highest priority element is always at the root. In this article, we will learn how to implement the max heap d 8 min read array::max_size() in C++ STL Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::max_size() This function returns the maximum number of elements that the array container can contain. In c 1 min read Templates in C++ C++ template is a powerful tool that allows you to write a generic code that can work with any data type. The idea is to simply pass the data type as a parameter so that we don't need to write the same code for different data types.For example, same sorting algorithm can work for different type, so 9 min read std::min_element in C++ The std::min_element() in C++ is an STL algorithm that is used to find the minimum element in a given range. This range can be array, vector, list or any other container. It is defined inside the <algorithm> header file. In this article, we will learn about the std::min_element() in C++.Exampl 4 min read STD::array in C++ The array is a collection of homogeneous objects and this array container is defined for constant size arrays or (static size). This container wraps around fixed-size arrays and the information of its size are not lost when declared to a pointer. In order to utilize arrays, we need to include the ar 5 min read Vector max_size() in C++ STL In C++, vector max_size() is a built-in function used to find the maximum number of elements that can be held by the vector container. In this article, we will learn about the vector max_size() function in C++.Let's take a look at an example that illustrates this method:CPP#include <bits/stdc++.h 3 min read stack top() in C++ STL In C++, the std::stack::top() is used to find the top element of the std::stack container. It is a member function of std::stack class defined inside the <stack> header file. In this article, we will learn how to find the top element of stack using stack::top() in C++.Example:C++#include <b 2 min read Like