How to swap Keys with Values of a Map in C++?
Examples:
Input: map = {'e', 1 }, {'o', 1 }, {'r', 3 }, Output: {1, 'e' }, {1, 'o' }, {3, 'r' },
- Method 1:
- By using vector pair, traverse through the given map
- push_back the swapped values in the vector pair
- sort the vector pair in ascending order
- in this case, we won’t get STL maps functionality since we are storing are result in a vector pair
Below is the implementation of the above approach:
// C++ program to swap keys
// and values of a map
// using Vector pair
#include <bits/stdc++.h>
using
namespace
std;
// Function to swap keys and values of a map
// and return a vector pair of the swapped values
template
<
typename
K,
typename
V>
vector<pair<V, K> >
invertMap2(map<K, V>
const
& myMap)
{
vector<pair<V, K> > myvec;
// traverse the map
// and pushback the values -> keys
for
(
auto
const
& pair : myMap) {
myvec.push_back(
make_pair(pair.second,
pair.first));
}
return
myvec;
}
// Function to call the swap operation
void
swap(map<
char
,
int
> char_frequency)
{
cout <<
"Original map:\n\n"
<<
"KEY Value\n"
;
for
(
auto
it
= char_frequency.begin();
it != char_frequency.end();
++it)
cout << it->first <<
" "
<< it->second <<
"\n"
;
// create a vector pair of int, char
// which store the swapped values
vector<pair<
int
,
char
> > freq_char
= invertMap2(char_frequency);
// sort the vector pair to get
// results similar to a map
sort(freq_char.begin(),
freq_char.end(),
[](pair<
int
,
char
>& a,
pair<
int
,
char
>& b) {
if
(a.first != b.first)
return
a.first < b.first;
return
a.second < b.second;
});
cout <<
"\nMap with Keys and"
<<
" Values swapped:\n\n"
<<
"KEY Value\n"
;
for
(
auto
it
= freq_char.begin();
it != freq_char.end();
++it)
cout << it->first <<
" "
<< it->second <<
"\n"
;
}
// Driver code
int
main()
{
// Taking the frequency map of
// w3wiki as the input map here
string input =
"w3wiki"
;
map<
char
,
int
> char_frequency;
for
(
int
i = 0; i < input.length(); ++i) {
char_frequency[input[i]]++;
}
// Swap the keys with values of this map
swap(char_frequency);
return
0;
}
Output:Original map: KEY Value e 4 f 1 g 2 k 2 o 1 r 1 s 2 Map with Keys and Values swapped: KEY Value 1 f 1 o 1 r 2 g 2 k 2 s 4 e
- Method 2: Another approach is to use STL MultiMap to store swapped map because Maps can’t have duplicate keys. We will traverse through the input map and insert the swapped values into the Multi-Map.
Below is the implementation of the above approach:
// C++ program to swap keys
// and values of a map
// using STL MultiMap
#include <bits/stdc++.h>
using
namespace
std;
// Function which swaps keys and
// values of a map and
// returns a multimap.
template
<
typename
K,
typename
V>
multimap<V, K>
invertMap(map<K, V>
const
& myMap)
{
multimap<V, K> myMultiMap;
// Traverse the map and
// pushback the values -> keys
for
(
auto
const
& pair : myMap) {
myMultiMap.insert(
make_pair(pair.second,
pair.first));
}
return
myMultiMap;
}
// Function to call the swap operation
void
swap(map<
char
,
int
> char_frequency)
{
cout <<
"Original map:\n\n"
<<
"KEY Value\n"
;
for
(
auto
it
= char_frequency.begin();
it != char_frequency.end();
++it)
cout << it->first <<
" "
<< it->second <<
"\n"
;
// create a multimap of int, char
multimap<
int
,
char
> freq_char
= invertMap(char_frequency);
cout <<
"\nMap with Keys and"
<<
" Values swapped:\n\n"
<<
"KEY Value\n"
;
for
(
auto
it
= freq_char.begin();
it != freq_char.end();
++it)
cout << it->first <<
" "
<< it->second <<
"\n"
;
}
// Driver code
int
main()
{
// Taking the frequency map of
// w3wiki as the input map here
string input =
"w3wiki"
;
map<
char
,
int
> char_frequency;
for
(
int
i = 0; i < input.length(); ++i) {
char_frequency[input[i]]++;
}
// Swap the keys with values of this map
swap(char_frequency);
return
0;
}
Output:Original map: KEY Value e 4 f 1 g 2 k 2 o 1 r 1 s 2 Map with Keys and Values swapped: KEY Value 1 f 1 o 1 r 2 g 2 k 2 s 4 e
Contact Us