# max_element in C++

We have std::max to find maximum of 2 or more elements, but what if we want to find the largest element in an array or vector or list or in a sub-section. To serve this purpose, we have std::max_element in C++.

**
std::max_element** is defined inside the header file

std::max_element can be used in two ways. The comparisons can be performed either using**operator < **(first version), or using a **pre-defined function **(second version). If more than one element satisfies the condition of being the largest, the iterator returned points to the first of such elements.

**The two versions are defined as given below:**

**For comparing elements using “<":**

Syntax:**template**ForwardIterator max_element (ForwardIterator first, ForwardIterator last); **first:**Forward iterator pointing to the beginning of the range.**last:**Forward iterator pointing to the end of the range.**Return Value:**It returns a pointer to the largest element in the range, and in case if there are more than one such element, then it points to the first one. It points to the last in case the range is empty.`// C++ program to demonstrate the use of std::max_element`

`#include <iostream>`

`#include <algorithm>`

`using`

`namespace`

`std;`

`int`

`main()`

`{`

`int`

`v[] = {`

`'a'`

`,`

`'c'`

`,`

`'k'`

`,`

`'d'`

`,`

`'e'`

`,`

`'f'`

`,`

`'h'`

`};`

`// Finding the maximum value between the first and the`

`// fourth element`

`int`

`* i1;`

`i1 = std::max_element(v, v + 4);`

`cout <<`

`char`

`(*i1) <<`

`"\n"`

`;`

`return`

`0;`

`}`

*chevron_right**filter_none*Output:

k

**For comparison based on a pre-defined function:**

Syntax:**template**Here, first and last are the same as previous case.ForwardIterator max_element (ForwardIterator first, ForwardIterator last, Compare comp); **comp:**Binary function that accepts two elements in the range as arguments, and returns a value convertible to bool. The value returned indicates whether the element passed as first argument is considered less than the second. The function shall not modify any of its arguments. This can either be a function pointer or a function object.**Return Value:**It returns a pointer to the largest element in the range, and in case if there are more than one such element, then it points to the first one. It points to the last in case the range is empty.`// C++ program to demonstrate the use of std::max_element`

`#include <iostream>`

`#include <algorithm>`

`using`

`namespace`

`std;`

`// Defining the BinaryFunction`

`bool`

`comp(`

`int`

`a,`

`int`

`b)`

`{`

`return`

`(a < b);`

`}`

`int`

`main()`

`{`

`int`

`v[] = { 9, 4, 7, 2, 5, 10, 11, 12, 1, 3, 6 };`

`// Finding the maximum value between the third and the`

`// ninth element`

`int`

`* i1;`

`i1 = std::max_element(v + 2, v + 9, comp);`

`cout << *i1 <<`

`"\n"`

`;`

`return`

`0;`

`}`

*chevron_right**filter_none*Output:

12

**Related Articles:**

This article is contributed by **Mrigendra Singh**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Different types of range-based for loop iterators in C++
- Learn C++ Programming Step by Step - A 20 Day Curriculum!
- Check if Array elements can be maximized upto M by adding all elements from another array
- Why do we need reference variables if we have pointers
- Similarities and Difference between Java and C++
- Macros and its types in C/C++
- How to find index of a given element in a Vector in C++
- Print all lexicographical greater permutations of a given string
- Introduction to Complex Objects and Composition
- NULL undeclared error in C/C++ and how to resolve it
- std::remove_const in C++ with Examples
- All possible values of floor(N/K) for all values of K
- Finding Median of unsorted Array in linear time using C++ STL
- Average of Cubes of first N natural numbers
- Top 10 Programming Languages for Blockchain Development