Search
 
SCRIPT & CODE EXAMPLE
 

CPP

maximum value in map in c++

#include <bits/stdc++.h>
using namespace std;

bool compare(const pair<int, int>&a, const pair<int, int>&b)
{
   return a.second<b.second;
}

int main(int argc, char const *argv[])
{
   int n, key, maxn;
   map<int,int> mp;

   cin>>n;

   for (int i=0; i<n; i++)
   {
     cin>>key;
     mp[key]++;
   }

   maxn = max_element(mp.begin(), mp.end(), compare)->second;

   cout<<maxn<<endl;

   return 0;
 }
Comment

maximum value in map in c++

auto x = std::max_element(m.begin(), m.end(),
    [](const pair<int, int>& p1, const pair<int, int>& p2) {
        return p1.second < p2.second; });
Comment

Maximum element in a map c++

// 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;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ threadpool 
Cpp :: c++ unordered_map initialize new value 
Cpp :: files c++ 
Cpp :: if else c++ 
Cpp :: C++ switch..case Statement 
Cpp :: bubble sort function in c++ 
Cpp :: && c++ 
Cpp :: unordered_map in c++ 
Cpp :: sstream c++ 
Cpp :: std::enable_shared_from_this include 
Cpp :: bool nullable to bool c# 
Cpp :: waiting in a serial as the spool reflect the queue operation. Demonstrate Printer Behavior in context of Queue.Subject to the Scenario implement the Pop and Push Using C++. 
Cpp :: error C2011 
Cpp :: pointers in cpp 
Cpp :: even number program in c++ using for loop stack overflow 
Cpp :: idnefier endl in undefince 
Cpp :: right rotation of array in c++ by one element 
Cpp :: qtextedit no line break 
Cpp :: C++ float and double simple example 
Cpp :: convert c++ to mips 
Cpp :: transpose function example in c++ 
Cpp :: . Single-line comments start with two forward slashes (//). 
Cpp :: delete item from linked list in c++ 
Cpp :: c++ to mips assembly converter 
Cpp :: qt get wireless interface name 
Cpp :: convert java to c++ 
Cpp :: max in c++ with three elements 
Cpp :: matrix chainmultiplication 
Cpp :: [3,2,4,-1,-4] 
Cpp :: multiple inheritance c++ 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =