How to Create a Stack of Map in C++? Last Updated : 12 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, the std::stack is a container that follows the LIFO (Last In, First Out) rule, whereas std::map is an associative container that stores key-value pairs. In this article, we will learn how to create a stack of a map in C++. Example:Input: myMap = { {1: ‘a’}, {2: ‘b’}, {3: ‘c’} }; myMap = { {4, ‘d’}, {5, ‘e’} }; Output: Stack of map: [ { {1: ‘a’}, {2: ‘b’}, {3: ‘c’} }, { {4, ‘d’}, {5, ‘e’} } ]Stack of Maps in C++We can create a stack of maps in C++ by passing the std::map type as the template argument during the declaration of the map. Syntax to Declare Stack of Maps in C++ stack< map<key_datatype, value_datatype>> stack_name; Here, key_datatype denotes the type of data you want to use as the key in the map. value_datatype denotes the type of data you want to store as the value in the map. stack_name is the name of the stack of map.C++ Program to Create Stack of MapThe below program demonstrates how we can create a stack of map in C++ STL. C++ // C++ Program to illustrate how to create a stack of maps #include <iostream> #include <map> #include <stack> using namespace std; int main() { // Define the type of map typedef map<int, string> MapType; // Initialize two maps MapType map1 = { { 1, "C++" }, { 2, "Java" } }; MapType map2 = { { 3, "Python" }, { 4, "JavaScript" } }; // Create a stack of maps stack<MapType> myStack; myStack.push(map1); myStack.push(map2); // Print the stack of maps while (!myStack.empty()) { MapType& topMap = myStack.top(); for (auto& pair : topMap) { cout << "{" << pair.first << ", " << pair.second << "}, "; } cout << endl; myStack.pop(); } return 0; } Output{3, Python}, {4, JavaScript}, {1, C++}, {2, Java}, Time Complexity: O(N), here N is the number of maps.Auxiliary Space: O(N * M), where M is the size of each map Comment More infoAdvertise with us Next Article How to Create a Stack of Multimap in C++? G gauravggeeksforgeeks Follow Improve Article Tags : C++ Programs C++ STL cpp-map cpp-stack CPP Examples +2 More Practice Tags : CPPSTL Similar Reads 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 Stack of Lists in C++? In C++, a list is a sequence container that allows dynamic insertion and deletion operations, whereas a stack is a data structure that follows last-in, first-out (LIFO). In this article, we will learn how to create a stack of lists in C++. Example: Input: list1 = { 1, 2, 3, 4 }list2 = { 5, 6, 7 }Out 2 min read How to Create a Stack of Set in C++? In C++ STL, 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. Sets are a type of associative container in which each element is unique and in some sorted order. In this arti 2 min read How to Create a Stack of Unordered_Map in C++? In C++, the stack is a container that follows the LIFO(Last In First Out) rule where new elements are added from one end (top) and removed from that end only. An unordered_map is an associative container that stores elements formed by a combination of key-value pairs, where the key should be unique. 2 min read How to Create a Stack of Multisets in C++? In C++, a stack is a container adapter that operates in a LIFO (Last In First Out) element order and allows insertion and deletion from the end only. A multiset is a container that stores elements in an ordered manner and allows multiple instances of an element. In this article, we will learn how to 2 min read How to Create a Stack of Stack in C++? In C++, the stack is a container that follows the LIFO (Last In, First Out) order in which the elements are inserted and removed from it. In this article, we will learn how to create a stack of a stack in C++. Example:Input:Elements in stack1= 1, 2, 3, 4Elements in stack2= 5, 6, 7Output:Elements in 2 min read Like