How to Store Vectors as Keys in a Multimap in C++?
Last Updated :
06 May, 2024
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,6};
Output:
myMultimapOfVectors = { {{1, 2, 3}, “first”},
{{4, 5, 6}, “second”},
{{7, 8, 9}, “third”} }
Store Vectors as Keys in a Multimap in C++
To store vectors as keys in a multimap in C++, we can define the type of std::multimap container keys as vector type during the declaration of the multimap. The below syntax shows how to do it.
Syntax to Declare Multimap with Vectors as Keys in C++
multimap< vector<dataType>, valueType> multimap_name;
Here,
datatype
denotes the type of data you want to store in the vector.mapped_datatype
denotes the type of data you want to store as the mapped value in the multimap.multimap_name
is the name of the multimap.
We can then use the multimap::insert or array subscript operator to insert data into the multimap.
Note: The multimap in C++ is implemented as Red-Black Trees and can work on data types that have valid <, >, == operators. So vectors can be stored as keys in it. While unordered_maps may requre a custom hash function to work on vectors.
C++ Program to Store Vectors as Keys in a Multimap
The below program demonstrates how we can store vectors as keys in a multimap in C++ STL.
C++
// C++ Program to illustrate how to store vectors as keys in
// a multimap
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main()
{
// Define the type of vector
typedef vector<int> VectorType;
// Initialize two vectors
VectorType vec1 = { 1, 2, 3 };
VectorType vec2 = { 4, 5, 6 };
// Create a multimap with vectors as keys
multimap<VectorType, char> myMultimap;
myMultimap.insert(make_pair(vec1, 'a'));
myMultimap.insert(make_pair(vec2, 'b'));
// Print the multimap with vectors as keys
for (auto& pair : myMultimap) {
cout << "{ ";
for (auto& element : pair.first) {
cout << element << ", ";
}
cout << "}, " << pair.second << endl;
}
return 0;
}
Output{ 1, 2, 3, }, a
{ 4, 5, 6, }, b
Time Complexity O(N), here N is the
number of elements in the multimap.
Auxiliary Space: O(N * M) where
M is the
size of the vectors.
Similar Reads
How to Sort a Vector in a Map in C++? In C++, we can create a map container where the values associated with keys is a vector. In this article, we will learn how to sort a vector within a map in C++. Example Input: myMap = { {3, {9, 7, 3}}, {5, {4, 2, 8, 1, 6}}, {8, {1, 2, 5, 8}} }; Output: Map: Key: 3, Sorted Vector: [3 7 9 ] Key: 5, S
2 min read
How to Create a Multimap of Vectors 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 vectors in C++. For Example, Input:myPa
2 min read
How to Store Vectors as Values in a Map? In C++, vectors are similar to arrays, but unlike arrays, the vectors are dynamic in nature. They can modify their size during the insertion or deletion of elements. On the other hand, Maps are containers that allow the users to store data in the form of key-value pairs. In this article, we will lea
2 min read
How To Find All Occurrences of a Key in a Multimap in C++? In C++, multimaps are associative containers similar to maps, but unlike maps, they can store multiple values mapped to the same key. In this article, we will learn how to find all the occurrences of a specific key in a multimap in C++. Example: Input:myMutimap = {{ "id", "111" }, { "id", "112" }, {
2 min read
How to Maintain the Order of a Vector in a Multiset in C++? In C++, multisets are associative containers that store elements in a sorted manner but unlike sets, multisets allow the users to store duplicate values as well and vectors are dynamic arrays that store data in contiguous memory locations. In this article, we will learn how to maintain the order of
3 min read