What is the Deque data structure in C++?

The double-ended queue or deque (pronounced “deck”) is a linear data structure, similar to a queue, that allows push and pop operations at both of its ends.

Let’s visualize the push and pop operations in a deque:

1 of 11

Member functions and operators


1. push_back() and push_front()

Both of these functions take in one argument: the element to be pushed into the deque. As their names suggest, the former pushes elements to the back of the deque, while the latter ​pushes elements to the front.

2. pop_back() and pop_front()

These functions take in no arguments. The former pops elements from the back of the deque, while the later pops them from the front.

#include <iostream>
#include <deque>
using namespace std;
int main(){
deque <int> deck; // deck = []
deck.push_back(1); // deck = [1]
deck.push_back(2); //deck = [1, 2]
deck.push_front(5); //deck = [5, 1, 2]
deck.push_front(6); // deck = [6, 5, 1, 2]
deck.pop_back(); // deck = [6, 5, 1]
deck.pop_front(); //deck = [5, 1]
}

3. front() and back()

These methods return the element at the front and back of the deque.

4. at() and []

Given an index, these methods let us access the element at that index in the deque.

void print_deque(deque<int> deck){
cout<<"[ ";
for(int i = 0; i < deck.size(); i++)
{
cout<< deck.at(i)<<" ";
}
cout<<"]";
}

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved