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



  1. Iterate the map entry by entry with help of iterators
    map::iterator itr;
    for (itr = some_map.begin();
         itr != some_map.end();
         ++itr) 
    {
        // operations
    }
    
  2. Store the first entry in a reference variable to compare to initially.
  3. If the current entry’s value is greater than the reference entry’s value, then store the current entry as the reference entry.
  4. Repeat this process for all the entries in the map.
  5. In the end, the reference variable has the required entry with the highest value in the map.
  6. Print this entry

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Output:

Map: 1 = 40, 2 = 30, 3 = 60, 
Entry with highest value: 3 = 60

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.