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 and it returns an iterator pointing to the element with the largest value in the range [first, last). std::max_element can be used in two ways. The comparisons can be performed either usingoperator < (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.
CPP
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int v[] = { 'a' , 'c' , 'k' , 'd' , 'e' , 'f' , 'h' };
int * i1;
i1 = std::max_element(v, v + 4);
cout << char (*i1) << "\n" ;
return 0;
}
|
Time Complexity: O(n)
Auxiliary Space: O(1)
2. For comparison based on a pre-defined function:
Syntax:
template
ForwardIterator max_element (ForwardIterator first, ForwardIterator last,
Compare comp);
Here, first and last are the same as previous case.
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.
CPP
#include <iostream>
#include <algorithm>
using namespace std;
bool comp( int a, int b)
{
return (a < b);
}
int main()
{
int v[] = { 9, 4, 7, 2, 5, 10, 11, 12, 1, 3, 6 };
int * i1;
i1 = std::max_element(v + 2, v + 9, comp);
cout << *i1 << "\n" ;
return 0;
}
|
Output:
12
Time complexity: O(n)
Auxiliary Space: O(1)
Related Articles:
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
11 Jun, 2022
Like Article
Save Article