C++ std::deque

Beginner
8 min read
Updated: Nov 2026
3 examples
std::deque

Definition and Usage

In C++, std::deque<T> is a container that holds a row of elements of type T. The word deque (say “deck”) means double-ended queue: you can add and remove elements at both the front and the back, and both are fast.

A deque also allows access by index, so d[2] and d.at(2) work just like in a std::vector. The difference is how the data is stored. A deque keeps its elements in several blocks, so it is not contiguous. Common functions are push_front(), push_back(), front(), and back(). Include <deque> to use it.

This page teaches what a std::deque is. The Deque Functions index lists every function guide in this series.

Why choose a deque?

Choose a deque when you need to add or remove at both ends, for example for a queue, a sliding window, or a history list. If you only add at the back, a std::vector is usually the simpler choice.

C++ Syntax

Common ways to create and use a std::deque:

C++
#include <deque>
#include <iostream>

std::deque<int> a;                  // empty deque
std::deque<int> b = {1, 2, 3};      // three elements
std::deque<int> c(5, 0);            // five zeros

b.push_front(0);                    // 0 1 2 3
b.push_back(4);                     // 0 1 2 3 4
int first = b.front();              // 0
int last  = b.back();               // 4
int x     = b[2];                   // 2, no check
b.pop_front();                      // 1 2 3 4
b.pop_back();                       // 1 2 3

std::deque vs std::vector vs std::list

The three containers look alike but behave differently. Here is a quick comparison:

Featurestd::vectorstd::dequestd::list
Add / remove at the backFastFastFast
Add / remove at the frontSlow (no push_front())FastFast
Access by index d[i]FastFastNot available
Insert in the middleSlowSlowFast (with an iterator)
Memory layoutOne blockSeveral blocksSeparate nodes
capacity() and reserve()YesNoNo

C++ Examples

Three short programs that show how std::deque works.

Example 1 — Add at Both Ends with push_front() and push_back()

A first program that adds to the front and the back, then reads both ends.

C++
#include <deque>
#include <iostream>

int main() {
    std::deque<int> d = {20, 30};

    d.push_front(10);
    d.push_back(40);

    std::cout << "front: " << d.front() << '\n';
    std::cout << "back: " << d.back() << '\n';
    std::cout << "size: " << d.size() << '\n';
    return 0;
}

How It Works

  1. std::deque<int> d = {20, 30} starts with two elements.
  2. push_front(10) adds 10 before them, and push_back(40) adds 40 after them. The deque is now 10 20 30 40.
  3. front() reads the first element, back() reads the last one, and size() counts all four.

Example 2 — Remove from Both Ends

pop_front() and pop_back() are both fast on a deque.

C++
#include <deque>
#include <iostream>

int main() {
    std::deque<int> d = {1, 2, 3, 4, 5};

    d.pop_front();
    d.pop_back();

    for (int n : d) {
        std::cout << n << ' ';
    }
    std::cout << "(size " << d.size() << ")\n";
    return 0;
}

How It Works

  1. The deque starts as 1 2 3 4 5.
  2. pop_front() removes 1 from the front, and pop_back() removes 5 from the back.
  3. Only the middle values 2 3 4 remain. No other element had to move, which is why both calls are fast.

Example 3 — Access by Index and Insert in the Middle

A deque works like a vector here: [] and at() are fast, and insert() can add in the middle.

C++
#include <deque>
#include <iostream>

int main() {
    std::deque<int> d = {10, 20, 40};

    std::cout << "d[1] = " << d[1] << '\n';
    std::cout << "d.at(2) = " << d.at(2) << '\n';

    d.insert(d.begin() + 2, 30);

    for (int n : d) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. d[1] reads the second element, 20, without any check. d.at(2) reads 40 and checks the index first.
  2. d.begin() + 2 is an iterator to the third element. Deque iterators support + because a deque has random access.
  3. insert() puts 30 before 40. Inserting in the middle moves elements, so it is slower than adding at the ends.

Next: C++ std::deque Functions Index

After you know what a std::deque is, open the function index for a full tutorial on each member function.

Start withWhy
push_front()Add an element at the front
push_back()Add an element at the back
pop_front()Remove the first element
at()Read an element with a bounds check

Full list: C++ std::deque Functions index (/cpp/deque/function).

Frequently Asked Questions

std::deque<T> is a container from <deque>. The name means double-ended queue. It can add and remove elements quickly at both the front and the back, and it allows access by index like a vector.
Both allow access by index. A vector stores everything in one block and is fast only at the back. A deque stores elements in several blocks and is fast at both the front and the back. A vector has no push_front().
A list is made of separate nodes, so it has no access by index, but it can insert in the middle quickly once you have an iterator. A deque allows d[i] and is faster to loop through in most programs.
No. A deque keeps its elements in several blocks. So you must not treat &d[0] as a plain array of all elements. Use a std::vector if you need one contiguous block.
Open the C++ std::deque Functions index. It lists every member function, from at() and push_front() to swap(), each with its own tutorial.

Browse every deque function

Open the functions index for categorized guides with examples and FAQs.

Deque Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful