![]() |
Multimap stores key-value pairs and for the same key, we can have more than one value i.e. multiple values for a single key. We can use this property of multimap to group vector of pair elements easily. In this article, we will discuss how to group elements of a vector of pairs in a multimap container. Example Input: myVector ={ {"A", 80},{"B", 70},{"A", 90},{"C", 90},{"B", 60},{"A", 100}} Output: myMultiMap = { {"A", 80},{"A", 90},{"A", 100},{"B", 60},{"B", 70},{"C", 90}} Grouping of Vector of Pair ElementsWe can just create a multimap of the same type as a pair and insert the vector elements in the map using the insert function. After that, we can access all the key-value pairs with the same key using the std::multimap::equal_range() function. C++ Program to Group Vector of Pairs Elements in a MultimapC++
Output All Key-Value pairs: Time Complexity: O(N log(N)) |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |