Sorting a Map by value in C++ STL
Last Updated :
29 Dec, 2022
Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have equal key values. By default, a Map in C++ is sorted in increasing order based on its key. Below is the various method to achieve this:
Method 1 – using the vector of pairs The idea is to copy all contents from the map to the corresponding vector of pairs and sort the vector of pairs according to second value using the lambda function given below:
bool cmp(pair<T1, T2>& a,
pair<T1, T2>& b)
{
return a.second < b.second;
}
where T1 and T2
are the data-types
that can be the
same or different.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<string, int >& a,
pair<string, int >& b)
{
return a.second < b.second;
}
void sort(map<string, int >& M)
{
vector<pair<string, int > > A;
for ( auto & it : M) {
A.push_back(it);
}
sort(A.begin(), A.end(), cmp);
for ( auto & it : A) {
cout << it.first << ' '
<< it.second << endl;
}
}
int main()
{
map<string, int > M;
M = { { "GfG" , 3 },
{ "To" , 2 },
{ "Welcome" , 1 } };
sort(M);
return 0;
}
|
Output
Welcome 1
To 2
GfG 3
Time Complexity: O(n*log(n)), where n is length of given map
Auxiliary Space: O(n)
Method 2 – using the set of pairs The idea is to insert all the (key-value) pairs from the map into a set of pairs that can be constructed using a comparator function that orders the pairs according to the second value.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
struct comp {
template < typename T>
bool operator()( const T& l, const T& r) const
{
if (l.second != r.second) {
return l.second < r.second;
}
return l.first < r.first;
}
};
void sort(map<string, int >& M)
{
set<pair<string, int >, comp> S(M.begin(), M.end());
for ( auto & it : S) {
cout << it.first << ' ' << it.second << endl;
}
}
int main()
{
map<string, int > M;
M = { { "GfG" , 3 }, { "To" , 2 }, { "Welcome" , 1 } };
sort(M);
return 0;
}
|
Output
Welcome 1
To 2
GfG 3
Time Complexity: O(n*log(n)), where n is the length of the given map
Auxiliary Space: O(n)
Method 3 – using multimap Multimap is similar to a map with an addition that multiple elements can have the same keys. Rather than each element is unique, the key-value and mapped value pair have to be unique in this case. The idea is to insert all pairs from the given map into multimap using the originals map’s value as a key in the multimap and original maps key value as a value in the multimap.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
void sort(map<string, int >& M)
{
multimap< int , string> MM;
for ( auto & it : M) {
MM.insert({ it.second, it.first });
}
for ( auto & it : MM) {
cout << it.second << ' ' << it.first << endl;
}
}
int main()
{
map<string, int > M;
M = { { "GfG" , 3 }, { "To" , 2 }, { "Welcome" , 1 } };
sort(M);
return 0;
}
|
Output
Welcome 1
To 2
GfG 3
Time Complexity: O(n*log(n)), where n is the length of the given map
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...