How to swap Keys with Values of a Map in C++?
Given a map, the task is to swap the Keys of this map with its values, 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>usingnamespacestd;// Function to swap keys and values of a map// and return a vector pair of the swapped valuestemplate<typenameK,typenameV>vector<pair<V, K> >invertMap2(map<K, V>const& myMap){vector<pair<V, K> > myvec;// traverse the map// and pushback the values -> keysfor(autoconst& pair : myMap) {myvec.push_back(make_pair(pair.second,pair.first));}returnmyvec;}// Function to call the swap operationvoidswap(map<char,int> char_frequency){cout <<"Original map:\n\n"<<"KEY Value\n";for(autoit= char_frequency.begin();it != char_frequency.end();++it)cout << it->first <<" "<< it->second <<"\n";// create a vector pair of int, char// which store the swaped valuesvector<pair<int,char> > freq_char= invertMap2(char_frequency);// sort the vector pair to get// results similar to a mapsort(freq_char.begin(),freq_char.end(),[](pair<int,char>& a,pair<int,char>& b) {if(a.first != b.first)returna.first < b.first;returna.second < b.second;});cout <<"\nMap with Keys and"<<" Values swapped:\n\n"<<"KEY Value\n";for(autoit= freq_char.begin();it != freq_char.end();++it)cout << it->first <<" "<< it->second <<"\n";}// Driver codeintmain(){// Taking the frequency map of// GeeksforGeeks as the input map herestring input ="geeksforgeeks";map<char,int> char_frequency;for(inti = 0; i < input.length(); ++i) {char_frequency[input[i]]++;}// Swap the keys with values of this mapswap(char_frequency);return0;}chevron_rightfilter_noneOutput: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>usingnamespacestd;// Function which swaps keys and// values of a map and// returns a multimap.template<typenameK,typenameV>multimap<V, K>invertMap(map<K, V>const& myMap){multimap<V, K> myMultiMap;// Traverse the map and// pushback the values -> keysfor(autoconst& pair : myMap) {myMultiMap.insert(make_pair(pair.second,pair.first));}returnmyMultiMap;}// Function to call the swap operationvoidswap(map<char,int> char_frequency){cout <<"Original map:\n\n"<<"KEY Value\n";for(autoit= char_frequency.begin();it != char_frequency.end();++it)cout << it->first <<" "<< it->second <<"\n";// create a multimap of int, charmultimap<int,char> freq_char= invertMap(char_frequency);cout <<"\nMap with Keys and"<<" Values swapped:\n\n"<<"KEY Value\n";for(autoit= freq_char.begin();it != freq_char.end();++it)cout << it->first <<" "<< it->second <<"\n";}// Driver codeintmain(){// Taking the frequency map of// GeeksforGeeks as the input map herestring input ="geeksforgeeks";map<char,int> char_frequency;for(inti = 0; i < input.length(); ++i) {char_frequency[input[i]]++;}// Swap the keys with values of this mapswap(char_frequency);return0;}chevron_rightfilter_noneOutput: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
Recommended Posts:
- Difference between std::swap and std::vector::swap
- map::at() and map::swap() in C++ STL
- multimap::swap() in C++ STL
- C Program to Swap two Numbers
- valarray swap() function in c++
- multimap swap() function in C++ STL
- TCS Coding Practice Question | Swap two Numbers
- C++ Program to swap two members using Friend Function
- How to traverse through all values for a given key in multimap?
- How to delete a range of values from the Set using Iterator
- How to return multiple values from a function in C or C++?
- Sums of ASCII values of each word in a sentence
- Rearrange Odd and Even values in Alternate Fashion in Ascending Order
- Count the number of words having sum of ASCII values less than and greater than k
- Program to find the product of ASCII values of characters in a string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : ManasChhabra2


