STL in C++

What is the implementation process for getting maximum value from a map in C++?

auto it=mp.end();
int ans = it->first;

This is for key

int val=0;
for(auto it=mp.begin();it!=mp.end();it++)

This is for value


Thanks a lot. And i wanna know another thing, If the maximum value is more than once in the map, how do I print the key for those maximum values?

Think this on your own first and try to search on net

Yeah okay.

int maxm = -1e9; // for storing the maximum value
for(auto it=m1.begin();it!=m1.end();it++){
maxm = it->second;
vector v; // vector v contains the keys which have maximum values
for(auto it=m1.begin();it!=m1.end();it++){

1 Like

Thank you so much.

1 Like

I’ve written the code for this input-output and i think the code is alright. As the map store the keys in ascending order so the output looks something like this. How could i solve this problem? Here is my code:


first, take input in the map for all the values then extract that key-value pair in a vector of pair<string,int> or pair<int,string> as you wish then sort it according to the int value by using sort() provide extra comparison function as parameter and then take top 1,2 or 3 or any number of values according to need

1 Like

Okay i’m trying to resolve this issue as you have said. Thanks.

you can use unordered map to avoid the alphabetically sorted thing

you can see more here

Yeah i’ve used unordered map. But got the same output.

Oh my bad… I didn’t know

Why doesn’t unordered map work here?

I can’t understand why it’s not working here.

what is the problem

1 Like

This is the problem which i am facing.

Do you expect others to derive the problem from sample input and output

1 Like

Yes i’do