How to Create a Stack of Deque in C++? Last Updated : 11 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 C++. Example: Input: myDeque1 = 1, 2, 3, 4myDeque2 = 5, 6, 7Output: Stack of Deque: [ {5, 6, 7}, {1, 2, 3, 4} ]Stack of Deque in C++To create a stack of deques in C++, we need to pass the std::deque as the template parameter in the declaration of the stack. We can then use the std::stack::push() function to insert the deque container in the stack. Syntax to Create a Stack of Deque in C++ stack<deque<datatype>> stack_name; Here, datatype denotes the type of data stored in the deque. stack_name is the name of the stack of deque. C++ Program to Use a Stack with a Deque The below program demonstrates how we can create a stack of deque in C++. C++ // C++ Program to illustrate how to crate a stack of deque #include <deque> #include <iostream> #include <stack> using namespace std; int main() { // Creating a stack of deques stack<deque<int> > stackOfDeques; // Pushing deques into the main stack (stackOfDeques) deque<int> d1, d2; d1.push_back(1); d1.push_back(2); d1.push_back(3); d1.push_back(4); d2.push_back(5); d2.push_back(6); d2.push_back(7); // push the deque in stack of deques stackOfDeques.push(d1); stackOfDeques.push(d2); // Printing elements from the stack of deques cout << "Elements in the Stack of Deque:" << endl; while (!stackOfDeques.empty()) { deque<int> currD = stackOfDeques.top(); stackOfDeques.pop(); while (!currD.empty()) { cout << currD.back() << " "; currD.pop_back(); } } return 0; } OutputElements in the Stack of Deque: 7 6 5 4 3 2 1 Time Complexity: O(N), here N is the total number of deque. Auxiliary Space: O(N * M), where M is the average number of elements in the deque. 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-deque CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Create a Deque of Sets in C++? In C++, a container called deque is a queue like container but allows for fast insertions and deletions at both ends. In this article, we will learn how to create a deque of sets in C++. Example: Input: mySet1 = {1, 4, 8, 9, 11} mySet2 = {1, 2, 3, 5, 7} Output: myDeque: [ {1, 4, 8, 9, 11}, {1, 2, 3, 2 min read How to Create a Deque of Vectors in C++? In C++, deques are sequence containers similar to queues but unlike queues, deques allow the insertion and deletion of elements from both ends efficiently. Vectors are dynamic arrays that can resize themselves during the runtime. In this article, we will learn how to create a deque of vectors in C++ 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 Deque of Maps in C++? In C++, the Standard Template Library (STL) provides a container called deque that allows efficient insertion and deletion operations at both ends of the container. A map is an associative container that stores elements in key-value pairs.In this article, we will learn how to create a deque of maps 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 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 Like