给定一个映射,任务是用C++交换此映射的键及其值。
例子:
Input: map =
{'e', 1 },
{'o', 1 },
{'r', 3 },
Output:
{1, 'e' },
{1, 'o' },
{3, 'r' },
- 方法1:
- 通过使用向量对,遍历给定的地图
- push_back向量对中交换的值
- 将向量对按升序排序
- 在这种情况下,我们将无法获得STL映射功能,因为我们存储的结果是矢量对
下面是上述方法的实现:
// C++ program to swap keys // and values of a map // using Vector pair #include
using namespace std; // Function to swap keys and values of a map // and return a vector pair of the swapped values template vector > invertMap2(map const& myMap) { vector > 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_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 swaped values vector > freq_char = invertMap2(char_frequency); // sort the vector pair to get // results similar to a map sort(freq_char.begin(), freq_char.end(), [](pair & a, pair & 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 // GeeksforGeeks as the input map here string input = "geeksforgeeks"; map 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; } 输出: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
- 方法2:另一种方法是使用STL MultiMap存储交换的地图,因为Map不能有重复的键。我们将遍历输入映射并将交换后的值插入到Multi-Map中。
下面是上述方法的实现:
// C++ program to swap keys // and values of a map // using STL MultiMap #include
using namespace std; // Function which swaps keys and // values of a map and // returns a multimap. template multimap invertMap(map const& myMap) { multimap 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_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 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 // GeeksforGeeks as the input map here string input = "geeksforgeeks"; map 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; } 输出: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
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。