Aggregate Multimap Values with Same Key and Store in Map in C++ Last Updated : 20 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, a multimap is an associative container that contains a sorted list of key-value pairs, while the same key can contain multiple values. It is a version of a map that allows duplicate keys. In this article, we will learn how to aggregate multimap values with the same key and store them in a map using C++. Example: Input: myMultimap = { {“apple”, 1}, {“banana”, 2}, {“apple”, 3}, {“banana”, 4}, {“apple”, 5}, {“banana”, 6} } Output: myMap = {“apple”: [1, 3, 5], “banana”: [2, 4, 6]}Aggregate Keys of Map in Vector For aggregating the values associated with a single key in the std::multimap and storing it in the map with the corresponding key, we can use the vector to store multiple values of the key as the value type for the map container. We can then insert the key-value pairs using the [] operators. C++ Program to Aggregate Multimap Keys in a VectorThe below example demonstrates how we can aggregate multimap keys in a vector in C++. C++ // C++ program to aggregate multimap values with the same // key and store them in a map #include <iostream> #include <map> #include <vector> using namespace std; int main() { // Multimap and map to be used multimap<string, int> myMultimap = { { "apple", 1 }, { "banana", 2 }, { "apple", 3 }, { "banana", 4 }, { "apple", 5 }, { "banana", 6 } }; map<string, vector<int> > myMap; // Aggregate multimap values with the same key for (auto& kv : myMultimap) { myMap[kv.first].push_back(kv.second); } // Print the map after aggregation cout << "Map after aggregation: " << endl; for (auto& kv : myMap) { cout << kv.first << ": "; for (int val : kv.second) { cout << val << " "; } cout << endl; } return 0; } OutputAggregated keys in the vector: 1 1 2 2 3 Time Complexity: O(N)Auxilliary Space: O(N) Comment More infoAdvertise with us Next Article How to swap Keys with Values of a Map in C++? R rajpootveerendrasingh36 Follow Improve Article Tags : C++ Programs C++ cpp-vector cpp-multimap CPP Examples +1 More Practice Tags : CPP Similar Reads 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 vect 4 min read 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 vect 4 min read How to Add Multiple Key-Value Pairs to a Map in C++? In C++, a map is a container that stores elements in key-value pairs, where each key is unique and associated with a value. In this article, we will learn how to add multiple key-value pairs to a map in C++. Example: Input: myMap = { {1, "one"} }; Elements to be insert = { {2, "two"}, {3, "three"}} 2 min read Whatâs the Best Way to Map Multiple Vectors to Keys in a Multimap in C++? In C++, multimaps are associative containers in which multiple values can be stored corresponding to the same key and vectors are dynamic arrays that store data in contiguous memory locations. In this article, we will learn what's the best way to map multiple vectors to keys in a multimap in C++. Ex 2 min read How to Store Vectors as Keys in a Multimap in C++? In C++, the std::multimap is a container that stores elements in a key-value pair, whereas std::vector is a sequence container that stores elements in contiguous memory. In this article, we will learn how to store vectors as keys in a multimap in C++. Example: Input:myVector ={1,2,3};myVector ={4,5, 2 min read How to Initialize a Map with a Range of Key-Value Pairs in C++? In C++, a map is an associative container that stores elements formed by combining a key and a value. In this article, we will learn how to initialize a map with a range of key-value pairs in C++. Initializing a Map with a Range of Key-Value Pairs in C++To initialize a std::map with a range of key-v 2 min read Like