![]() |
VOOZH | about |
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”} } ]
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.
stack < multimap <keyType, valueType> myStack;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.