How to find the Entry with largest Value in a C++ Map
Given a map in C++, the task is to find the entry in this map with the highest value.
Examples:
Input: Map = {ABC = 10, DEF = 30, XYZ = 20}
Output: DEF = 30
Input: Map = {1 = 40, 2 = 30, 3 = 60}
Output: 3 = 60
Approach
- Iterate the map entry by entry with help of iterators
map
::iterator itr; for (itr = some_map.begin(); itr != some_map.end(); ++itr) { // operations } - Store the first entry in a reference variable to compare to initially.
- If the current entry’s value is greater than the reference entry’s value, then store the current entry as the reference entry.
- Repeat this process for all the entries in the map.
- In the end, the reference variable has the required entry with the highest value in the map.
- Print this entry
Below is the implementation of the above approach:
// C++ program to find the Entry // with largest Value in a Map #include <bits/stdc++.h> using namespace std; // Function to print the Map void printMap(map<int, int> sampleMap) { map<int, int>::iterator itr; for (itr = sampleMap.begin(); itr != sampleMap.end(); ++itr) { cout << itr->first << " = " << itr->second << ", "; } cout << endl; } // Function tp find the Entry // with largest Value in a Map pair<int, int> findEntryWithLargestValue( map<int, int> sampleMap) { // Reference variable to help find // the entry with the highest value pair<int, int> entryWithMaxValue = make_pair(0, 0); // Iterate in the map to find the required entry map<int, int>::iterator currentEntry; for (currentEntry = sampleMap.begin(); currentEntry != sampleMap.end(); ++currentEntry) { // If this entry's value is more // than the max value // Set this entry as the max if (currentEntry->second > entryWithMaxValue.second) { entryWithMaxValue = make_pair( currentEntry->first, currentEntry->second); } } return entryWithMaxValue; } // Driver code int main() { // Map map<int, int> sampleMap; sampleMap.insert(pair<int, int>(1, 40)); sampleMap.insert(pair<int, int>(2, 30)); sampleMap.insert(pair<int, int>(3, 60)); // Printing map cout << "Map: "; printMap(sampleMap); // Get the entry with largest value pair<int, int> entryWithMaxValue = findEntryWithLargestValue(sampleMap); // Print the entry cout << "Entry with highest value: " << entryWithMaxValue.first << " = " << entryWithMaxValue.second << endl; return 0; } |
chevron_right
filter_none
Output:
Map: 1 = 40, 2 = 30, 3 = 60, Entry with highest value: 3 = 60
Recommended Posts:
- Largest hexagon that can be inscribed within a square
- Largest number less than or equal to N/2 which is coprime to N
- Largest sub-set possible for an array satisfying the given condition
- Largest right circular cylinder within a cube
- C++ Program for Largest K digit number divisible by X
- Largest hexagon that can be inscribed within an equilateral triangle
- Volume of largest right circular cylinder within a Sphere
- Largest right circular cylinder that can be inscribed within a cone which is in turn inscribed within a cube
- Largest square that can be inscribed within a hexagon which is inscribed within an equilateral triangle
- Largest right circular cone that can be inscribed within a sphere which is inscribed within a cube
- Largest sphere that can be inscribed in a right circular cylinder inscribed in a frustum
- std::find in C++
- unordered_map find in C++ STL
- multimap find() in C++ STL
- set find() function in C++ STL
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

