VOOZH about

URL: https://www.geeksforgeeks.org/cpp/stack-of-multimaps-in-cpp/

⇱ How to Create a Stack of Multimap in C++? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Create a Stack of Multimap in C++?

Last Updated : 23 Jul, 2025

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 create a stack of multimaps in C++.

Example:

Input: 
myMultimap1 = { {1, “C++”}, {2, “Java”}, {1, “Python”} };
myMultimap2 = { {2, “JavaScript”} };
Output: 
myStack: [ { {1, “C++”}, {2, “Java”}, {1, “Python”} },
 { {2, “JavaScript”} } ]

Stack of Multimaps in C++

To create a stack of multimaps in C++, we have to define the type of stack elements as a multimap in the template definition. The stack can store any type of data, including complex data types like multimaps.

Syntax to Declare Stack of Multimap

stack < multimap <keyType, valueType> myStack;

C++ Program to Create a Stack of Multimaps


Output
myStack:
[ { {1, Python}, {2, JavaScript}, } { {1, C++}, {2, Java}, } ]

Time Complexity: O(N) where n is the number of multimaps.
Auxiliary Space: O(N * M), where M is the average size of the multimaps.



Comment