VOOZH about

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

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


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 23 Jul, 2025

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, 4
myDeque2 = 5, 6, 7
Output:
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++.


Output
Elements 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