The std::max_element() function in C++ is an STL algorithm used to find the maximum element within a 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:
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
vector<int> v = {2, 1, 17, 10};
int arr[4] = {33, 87, 1, 71};
int n = sizeof(arr)/sizeof(arr[0]);
// Max element in vector
cout << *max_element(v.begin(), v.end()) << endl;
// Max element in array
cout << *max_element(arr, arr + n);
return 0;
}
Output
17 87
Syntax
std::max_element(first, last, comp);
Parameters
- first: Iterator to the first element of the range.
- last: Iterator to the element just after the last element of the range.
- comp: Binary comparator function, functor or lambda expression that compares two elements in the range. By default, it is set as < operator.
Return Value
- Returns an iterator to the largest element in the range.
- When the range is empty, it returns iterator to the last.
Example 1: Find Maximum Element in Array
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
int arr[] = {33, 87, 1, 71};
int n = sizeof(arr)/sizeof(arr[0]);
// Finding the maximum element in array
cout << *max_element(arr, arr + n);
return 0;
}
Output
87
Example 2: Finding Element Having Maximum Remainder After Division with 5
For this purpose, we have to use the custom comparator function.
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// Function that returns true if the first
// argument have smaller remainder left after
// division with 5
bool comp(int a, int b) {
return a % 5 < b % 5;
}
int main() {
vector<int> v = {33, 87, 1, 71};
// Finding the maximum element in given
// range of vector
cout << *max_element(v.begin(), v.end(),
comp);
return 0;
}
Output
33
Example 3: Find Maximum Element in Vector of User Defined Data Type
We have to use custom comparator again to determine how to compare user defined data type on any of its property.
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct St {
string name;
int sno;
};
int main() {
// Create a vector of structure
vector<St> v = {{"Ashok", 11}, {"Deepak", 15},
{"Anmol", 23}, {"Vikas", 19}};
// Find the maximum element in the vector of structure
// based on the sno field
St max = *max_element(v.begin(), v.end(),
[](const St &i, const St &j) {
return i.sno < j.sno;
});
cout << max.name << " " << max.sno;
return 0;
}
Output
Anmol 23
How std::max_element() Works Internally
1. It starts by assuming the first element is the maximum.
2. It then iterates sequentially from first to last.
3. For each element, it compares it with the current maximum using:
- operator< (default), or
- the user-defined comparator.
4. If a larger element is found, the iterator to that element becomes the new maximum.
5. After reaching the end, it returns an iterator pointing to the largest element.