How to find the Entry with largest Value in a C++ Map - GeeksforGeeks

17 Mar, 2020
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 iteratorsmap::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
Iterate the map entry by entry with help of iteratorsmap::iterator itr;
for (itr = some_map.begin();
     itr != some_map.end();
     ++itr) 
{
    // operations
}

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 Mapvoid 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 Mappair<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 codeint 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;}
Map: 1 = 40, 2 = 30, 3 = 60, 
Entry with highest value: 3 = 60

cpp-map
C++
C++ Programs
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Friend class and function in C++
Sorting a vector in C++
std::string class in C++
Header files in C/C++ and its uses
Program to print ASCII Value of a character
How to return multiple values from a function in C or C++?
C++ Program for QuickSort
Sorting a Map by value in C++ STL