Open In App

How to Create Deque of Multimap in C++?

Last Updated : 01 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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;
}

Output
myDeque: 
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)




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads