What Is Deque Data Structure in C++?


Angela Bailey

Deque Data Structure in C++

Deque stands for Double-Ended Queue. It is a linear data structure that allows insertion and deletion of elements from both ends – front and rear. Unlike stacks and queues, which only allow insertion and deletion from one end, a deque provides more flexibility.

Why Use a Deque?

A deque offers the following advantages:

  • Efficient Insertion and Deletion: Elements can be added or removed from both ends in constant time, O(1).
  • Versatility: A deque can be used as a stack or a queue by manipulating its ends.
  • Random Access: Elements in a deque can be accessed by their index, allowing for efficient random access operations.

C++ Implementation of Deque

In C++, the deque data structure is defined in the <deque> header file. To use it in your program, you need to include this header file.

Create a Deque

To create a deque, you can use the following syntax:

std::deque<T> myDeque;

Here, T represents the type of elements you want to store in the deque. For example, if you want to store integers, you can create a deque like this:

std::deque<int> myDeque;

Add Elements to the Deque

You can add elements to the front or back of the deque using the push_front() and push_back() member functions respectively.

myDeque.push_front(element); // Add element to the front of the deque
myDeque.push_back(element); // Add element to the back of the deque

Remove Elements from the Deque

To remove elements from the front or back of the deque, you can use the pop_front() and pop_back() member functions respectively.pop_front(); // Remove element from the front of the deque
myDeque.pop_back(); // Remove element from the back of the deque

Accessing Elements in a Deque

Elements in a deque can be accessed using indexing, similar to an array. The indexing starts from 0.

T element = myDeque[index]; // Access element at index

Additional Operations

The C++ deque also provides other useful member functions like size(), empty(), and clear().

  • size(): Returns the number of elements in the deque.
  • empty(): Checks if the deque is empty.
  • clear(): Removes all elements from the deque, leaving it empty.

A Sample Deque Program:

Let’s see an example that demonstrates how to use a deque in C++:

#include <iostream>
#include <deque>

int main() {
    std::deque<int> myDeque;


    std::cout << "Front element: " << myDeque.front() << std::endl;
    std::cout << "Back element: " << myDeque.back() << std::endl;


    std::cout << "Size of deque: " << myDeque.size() << std::endl;

    return 0;

This program creates a deque, adds elements to it, prints the front and back elements, removes an element from the front, and prints the size of the deque.


In conclusion, a deque is a versatile data structure that allows efficient insertion and deletion from both ends. It can be used as a stack or a queue depending on your requirements. C++ provides a built-in deque container in the <deque> header file, making it easy to work with deques in your programs.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy