How to Create a Multimap of Vectors in C++?
Last Updated :
27 Feb, 2024
In C++, a multimap is similar to a map with the addition that multiple elements can have the same keys. Also, it is not required that the key-value and mapped value pair have to be unique in this case. In this article, we will learn how to create a multimap of vectors in C++.
For Example,
Input:
myPair1 = {1, {2, 3, 4}}
myPair2 = {2, {5, 6, 7}}
Output:
MultiMap = { {1, {2, 3, 4}}, {2, {5, 6, 7}} }
Creating a Multimap of Vectors in C++
To create a multimap of vectors, we will have to first declare a multimap where in each key-value pair, the key is of type string and the value is a vector.
We can then use the std::multimap::insert() function to insert key-value pairs into the multimap. The key is a string, and the value is a vector.
Syntax to Create a Multimap of Vectors
multimap<string, vector<int> > myMultimap;
C++ Program to Create a Multimap of Vectors
C++
// C++ program to create a multimap of vectors
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main()
{
// Creating a multimap of string and vector
multimap<string, vector<int> > myMultimap;
// Inserting key-value pairs into the multimap
myMultimap.insert({ "apple", { 1, 2 } });
myMultimap.insert({ "banana", { 2, 3, 4 } });
myMultimap.insert({ "cherry", { 3, 4, 5, 6 } });
myMultimap.insert({ "apple", { 1, 2 } });
// Displaying the multimap elements
for (auto it = myMultimap.begin();
it != myMultimap.end(); ++it) {
cout << it->first << " ";
for (auto vec_it = it->second.begin();
vec_it != it->second.end(); ++vec_it) {
cout << *vec_it << " ";
}
cout << endl;
}
return 0;
}
Outputapple 1 2
apple 1 2
banana 2 3 4
cherry 3 4 5 6
Time Complexity: O(N * log N), where N is the number of arrays.
Space Complexity: O(N * M), where M is the average size of the arrays.
Similar Reads
How to Create a Stack of Multimap in C++? In C++, Stacks are a type of container adaptor with LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. A multimap is a container that stores key-value pairs in an ordered manner. In this article, we will learn how to c
2 min read
How to Create a Set of Vectors in C++? In C++, a set is a type of associative container in which duplicate elements are not allowed and a vector is a dynamic array in which duplicate elements are allowed. In this article, we will learn how to create a set of vectors in C++. For Example, Input:vector<int> vec1={1, 2, 3};vector<in
2 min read
How to Create a Multimap of Arrays in C++? In C++, a multimap is similar to a map with the addition that multiple elements can have the same keys. Also, it is NOT required that the key-value and mapped value pair have to be unique in this case. In this article, we will learn how to create a multimap of arrays in C++ STL. Example Input: myArr
2 min read
How to Create a Unordered Multimap of Tuples in C++? In C++, an unordered multimap container stores key-value pairs in no particular order. Unlike a map, an unordered multimap allows multiple values to be associated with a single key. In this article, we will learn how to create an unordered multimap of tuples in C++. For Example, Input: myPair1 = { "
2 min read
How to Create a Stack of Unordered_Multimap in C++? In C++, an unordered_multimap is an associative container that contains key-value pairs allowing multiple elements with the same key. In this article, we will learn how to create a stack of unordered_multimaps in C++. Example: Input: myMultimap1 = { {1, âC++â}, {2, âJavaâ}, {1, âPythonâ} }; myMultim
2 min read