ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

How to Sort STL Map in C++ in Increasing Order according to its Key Value?

user-image
تم إضافة السؤال من قبل Harshit Pandey , System Software Engineer , Vihaan Networks Limited
تاريخ النشر: 2015/02/27
Ahmad Ayaz
من قبل Ahmad Ayaz , Trainee Engineer , Zealcon Engineering (Pvt.) Ltd.

By default std::map is already sorted in increasing order. The third template parameter of a std::map, is a Compare functor wich is equal to std::less

Danny Kabrane
من قبل Danny Kabrane , CAD Software Developer , AiM Services

By default std::map is already sorted in increasing order. The third template parameter of a std::map, is a Compare functor wich is equal to std::less, if you want on the other hand decreasing order use std::greater for the third template parameter.

e.g :

std::map<int, int, std::greater> m;

I wonder how can I implement the STL map sorting by value.

For example, I have a map m

map<int,int>; m[1]=; m[2]=5; m[4]=6; m[6]=1;

and then.. I'd like to sort that with the m's value. So, if I print the map, I'd like to get the result like

m[6]=1 m[2]=5 m[4]=6 m[1]=

this.

المزيد من الأسئلة المماثلة

هل تحتاج لمساعدة في كتابة سيرة ذاتية تحتوي على الكلمات الدلالية التي يبحث عنها أصحاب العمل؟