This post will discuss how to find an element with the maximum value in a map in C++.

1. Using std::max_element

The recommended solution is to use the std::max_element to find an element having the maximum value in a map. It returns an iterator pointing to an element with the maximum value in the specified range. It is defined in the <algorithm> header.

Download  Run Code

 
Here’s a generic version using templates.

Download  Run Code

2. Using Loop

Alternatively, we can write a custom function for finding an element with the maximum value in the map. It can be implemented as follows in C++.

Download  Run Code

That’s all about finding the element with the maximum value in a map in C++.