How to Create a Stack of Stack in C++? Last Updated : 07 May, 2024 Comments Improve Suggest changes Like Article Like Report 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 the Stack of Stacks:7 6 5 4 3 2 1 Stack of Stacks in C++To create a stack of stack in C++, we can define the type of stack elements to be another stack by passing the std::stack type as a template parameter. We can then push other stacks using the std::stack::push() function and remove them using the std::stack::pop() function. Syntax to Declare Stack of Stacks in C++stack<stack<datatype>> stack_name; Here, datatype denotes the type of data stored in the stack.stack_name is the name of the stack of stack.C++ Program to Create Stack of StackThe below program demonstrates how we can create a stack of stack in C++ STL. C++ // C++ Program to illustrate how to create a stack of stacks #include <iostream> #include <stack> using namespace std; int main() { // Define the type of stack typedef stack<int> StackType; // Initialize two stacks StackType stack1, stack2; stack1.push(1); stack1.push(2); stack1.push(3); stack2.push(4); stack2.push(5); // Create a stack of stacks stack<StackType> myStack; myStack.push(stack1); myStack.push(stack2); // Print the stack of stacks while (!myStack.empty()) { StackType& topStack = myStack.top(); while (!topStack.empty()) { cout << topStack.top() << ", "; topStack.pop(); } cout << endl; myStack.pop(); } return 0; } Output5, 4, 3, 2, 1, Time Complexity: O(N), where N is the total number of stack.Auxiliary Space: O(N * M), where M is the size of each stack. Comment More infoAdvertise with us Next Article How to Create a Stack of Stack in C++? G gauravggeeksforgeeks Follow Improve Article Tags : C++ Programs C++ STL cpp-stack CPP Examples +1 More Practice Tags : CPPSTL Similar Reads 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 Strings in C++? In C++, Stacks are a type of container adaptor with a 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. In this article, we will learn how to create a stack of strings in C++. Creating a Stack of Strings in C++To crea 1 min read How to Create a Stack of Pairs 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 pair is a simple container that stores data in a key and value format. In this article, we will learn how to crea 2 min read How to Create a Stack of Vectors in C++? In C++, a stack of vectors can be created using the Standard Template Library (STL). The stack is a container adapter that provides a Last-In-First-Out (LIFO) type of data structure, and a vector is a dynamic array that can grow and shrink in size. In this article, we will learn how to create a stac 2 min read How to Create a Stack of Queue in C++? In C++, the std::stack is a container that follows the LIFO (Last In, First Out) rule, whereas std::queue is a container that follows the FIFO (First In, First Out) rule. In this article, we will learn how to create a stack of a queue in C++. Example: Input:myQueue = { a, b, c };myQueue = { d, e };O 2 min read How to Create a Stack of Map in C++? 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 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 Deque in C++? In C++, the stack is a container in which new elements are added from one end (top) and removed from that end only whereas a deque (double-ended queue) are sequence container with the feature of expansion and contraction on both ends. In this article, we will learn how to create a stack of deque in 2 min read How to Create a Stack of Arrays in C++? In C++, the std::stack is a container that follows the LIFO (Last In, First Out) rule, whereas std::array is a sequence container that stores elements in contiguous memory. In this article, we will learn how to create a stack of an array in C++. Example: Input: arr1 = {1, 2, 3}; arr2 = {4, 5, 6}; ar 2 min read How to Create Stack of Tuples in C++? In C++, a stack is a container adapter that provides a LIFO (Last In First Out) order of element insertion and deletion which is only possible at the end. A tuple is a container that can store elements of different types. In this article, we will learn how to create a stack of tuples in C++. Example 2 min read Like