How to Create Deque of Multimap in C++?
Last Updated :
01 Apr, 2024
In C++, a deque (double-ended queue) is a data structure that allows insertion and deletion at both ends, while a multimap is an associative container that contains key-value pairs, where multiple keys can have the same value. In this article, we will learn how to create a deque of multimaps in C++ STL.
Example:
Input:
myMultimap1 = { {1, "apple"}, {2, "banana"}, {1, "mango"} }
myMultimap2 = { {3, "grapes"}, {4, "orange"}, {3, "kiwi"} }
Output:
myDeque: [ { {1, "apple"}, {2, "banana"}, {1, "mango"} },
{ {3, "grapes"}, {4, "orange"}, {3, "kiwi"} } ]
Creating Deque of Multimap in C++
To create a std::deque
of std::multimap
in C++, we can pass the type of the deque container to be of the type std::multimap
as a template parameter while declaration.
Syntax to Create a Deque of Multimaps in C++
deque<multimap<dataType1, dataType2>> dequeName;
Here,
dataType1
denotes the data type of the key stored in the multimap.dataType2
denotes the data type of the value stored in the multimap.dequeName
is a name of deque of multimaps.
C++ Program to Create Deque of Multimap
The below example demonstrates how we can create a deque of multimaps in C++.
C++
// C++ Program to illustrate how to create deque of multimap
#include <deque>
#include <iostream>
#include <map>
using namespace std;
int main()
{
// Creating a deque of multimaps
deque<map<int, string> > dequeMulMap;
// Creating multimaps and adding elements to them
map<int, string> mmap1
= { { 1, "apple" }, { 2, "banana" } };
map<int, string> mmap2
= { { 3, "orange" }, { 4, "grape" } };
// Pushing multimaps into the deque
dequeMulMap.push_back(mmap1);
dequeMulMap.push_back(mmap2);
// Displaying the deque
cout << "myDeque: " << endl;
int i = 1;
for (auto& ele : dequeMulMap) {
cout << "Multimap" << i++ << ": ";
for (auto& pair : ele) {
cout << "{" << pair.first << ", " << pair.second
<< "} ";
}
cout << endl;
}
return 0;
}
OutputmyDeque:
Multimap1: {1, apple} {2, banana}
Multimap2: {3, orange} {4, grape}
Time Complexity: O(N * M), where N is the number of maps and M is the average size of each map.
Auxilliary Space: O(N * M)
Similar Reads
How to Create Deque of Multiset in C++? In C++, deques are containers that allow efficient insertion and deletion operations from both ends. Multisets are associative containers similar to sets but unlike sets, multisets allow duplicate insertions as well. In this article, we will learn how to create a deque of multiset in C++. Example In
2 min read
How to Create a Deque of Maps in C++? In C++, the Standard Template Library (STL) provides a container called deque that allows efficient insertion and deletion operations at both ends of the container. A map is an associative container that stores elements in key-value pairs.In this article, we will learn how to create a deque of maps
2 min read
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 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 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 Create a Deque of Vectors in C++? In C++, deques are sequence containers similar to queues but unlike queues, deques allow the insertion and deletion of elements from both ends efficiently. Vectors are dynamic arrays that can resize themselves during the runtime. In this article, we will learn how to create a deque of vectors in C++
2 min read