ডিফল্টভাবে ম্যাপ তার "কি" অনুসারে ডাটা সর্ট করে। এটা পরিবর্তন করার কোনো উপায় নাই।
তবে ম্যাপের ডাটাকে multimap
-এ কপি করে এইভাবে সর্ট করা যায়-
#include < iostream >
#include < vector >
#include < map >
using namespace std;
int main()
{
map <char, int> my_map;
my_map['b'] = 20;
my_map['a'] = 30;
my_map['c'] = 10;
// print map
cout << "Sort by key" << endl;
for(auto const it : my_map) {
cout << it.first << " => " << it.second << endl;
}
multimap <int, char> mm;
// insert data to multimap
for(auto const &item : my_map) {
mm.insert(make_pair(item.second, item.first));
}
// print value
cout << "Sort by value" << endl;
for(auto const &item : mm) {
cout << item.second << " => " << item.first << endl;
}
return 0;
}
আউটপুটঃ
Sort by key
a => 30
b => 20
c => 10
Sort by value
c => 10
b => 20
a => 30
answered
28 Jan, 15:23
menon
4.7k●3●34