Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Deque (Double-ended Queue) in C++

C++ deque (double-ended queue) basics: push and pop at the front and back, index with [] and at(), insert and erase in the middle, and how it differs from vector.

IntermediateSTL Containers ProgramsExample 10 of 10
deque-double-ended-queue.cpp
1#include <iostream>
2#include <deque>
3using namespace std;
4
5int main() {
6 // Create deque
7 deque<int> dq;
8
9 // Add elements at both ends
10 dq.push_back(10);
11 dq.push_back(20);
12 dq.push_back(30);
13 dq.push_front(5);
14 dq.push_front(1);
15
16 cout << "Deque elements: ";
17 for (int num : dq) {
18 cout << num << " ";
19 }
20 cout << endl;
21
22 cout << "Front: " << dq.front() << endl;
23 cout << "Back: " << dq.back() << endl;
24 cout << "Size: " << dq.size() << endl;
25
26 // Access elements by index
27 cout << "Element at index 2: " << dq[2] << endl;
28 cout << "Element at index 2 (at): " << dq.at(2) << endl;
29
30 // Remove from front
31 dq.pop_front();
32 cout << "\nAfter pop_front: ";
33 for (int num : dq) {
34 cout << num << " ";
35 }
36 cout << endl;
37
38 // Remove from back
39 dq.pop_back();
40 cout << "After pop_back: ";
41 for (int num : dq) {
42 cout << num << " ";
43 }
44 cout << endl;
45
46 // Insert at specific position
47 dq.insert(dq.begin() + 1, 15);
48 cout << "\nAfter inserting 15 at index 1: ";
49 for (int num : dq) {
50 cout << num << " ";
51 }
52 cout << endl;
53
54 // Erase element
55 dq.erase(dq.begin() + 1);
56 cout << "After erasing element at index 1: ";
57 for (int num : dq) {
58 cout << num << " ";
59 }
60 cout << endl;
61
62 return 0;
63}

Sample Output

Deque elements: 1 5 10 20 30

5, then 1, were pushed at the front.

After pop_back: 5 10 20

30 was removed from the back.

In this one

  • How a deque grows at both ends
  • How index access works on a deque
  • Which deque operations are O(1) and which are O(n)
  • When to pick deque over vector

How it works

  1. 1

    push_back 10, 20, 30; push_front 5, then 1.

  2. 2

    Print 1 5 10 20 30 with front, back, and size.

  3. 3

    Read index 2 with [] and at(): 10.

  4. 4

    pop_front → 5 10 20 30; pop_back → 5 10 20.

  5. 5

    insert 15 at index 1, then erase it.

Edge cases

Pop on an empty deque

Call pop_front() on an empty deque.

Undefined behavior. Check empty() before popping.

Pointer stability

Store &dq[0], then push_front.

Iterators are invalidated by push at either end, but references to existing elements stay valid. That rule is different from vector, where growth invalidates everything.

What's going on

A deque (pronounced "deck", short for double-ended queue) is a sequence you can grow and shrink cheaply at both ends. A vector is fast only at the back; a deque is fast at the front too.

The program pushes 10, 20, 30 at the back and then 5 and 1 at the front. Each push_front goes before everything else, so 1 ends up first: 1 5 10 20 30. front() is 1, back() is 30, and size() is 5.

Unlike a stack or queue, a deque has index access. dq[2] and dq.at(2) both give 10; at checks the index and throws on a bad one.

pop_front() removes 1, and pop_back() removes 30, leaving 5 10 20. Both are O(1).

insert(dq.begin() + 1, 15) puts 15 at index 1 and erase(dq.begin() + 1) takes it out. Inserting or erasing in the middle is O(n), as with a vector.

The trick behind the deque is its layout. It is not one contiguous block. It keeps a set of fixed-size chunks plus a small table pointing to them, so it can add a new chunk at either end without moving existing elements. The price is that indexing goes through that table, which is slightly slower than a vector, and you cannot hand &dq[0] to code that expects a plain array.

Use a deque for sliding windows, for undo buffers capped at a size, or whenever you need both ends. stack and queue use a deque underneath by default.

The headers come first. <iostream> is there because it provides cout and cin for input/output operations. <deque> is there because it provides deque container class. Leave one out and the compiler stops at the first name it does not recognize.

Understanding Deque

Double-ended Queue Concept:

Insert/delete at both ends
Random access like vectors
Efficient front and back operations
Collection of memory blocks

Key Features:

O(1) operations at both ends
Random access (like arrays)
Flexible insertion/deletion
Combines vector and queue benefits

Adding Elements

At Back:

dq.push_back(10); // Add to end

At Front:

dq.push_front(5); // Add to beginning

What happens here:

push_back(): adds to end (like vector)
push_front(): adds to beginning (unique to deque)
Both O(1) operations
Flexible insertion

Removing Elements

From Back:

dq.pop_back(); // Remove from end

From Front:

dq.pop_front(); // Remove from beginning

What happens here:

pop_back(): removes from end
pop_front(): removes from beginning
Both O(1) operations
Flexible deletion

Accessing Elements

Random Access:

dq[2] // Element at index 2

dq.at(2) // Element at index 2 (with bounds check)

Front and Back:

dq.front() // First element

dq.back() // Last element

What happens here:

[] operator: array-like access
at(): bounds-checked access
front()/back(): end elements
O(1) access

Inserting at Position

Using insert():

dq.insert(dq.begin() + 1, 15);

What happens here:

Inserts at specified position
Uses iterator arithmetic
O(n) operation (may shift elements)
Flexible but expensive for large deques

When to Use Deque

Good fits:

Need both stack and queue operations
Random access required
Efficient front and back operations
Sliding window problems
Both ends manipulation

Example Scenarios:

Sliding window maximum
Palindrome checking
Both ends processing
Queue with random access
Double-ended operations

Important Considerations

Performance:

Front/back operations: O(1)
Random access: O(1)
Middle insert/erase: O(n)
More efficient than vector for front operations

Memory Layout:

Collection of memory blocks
Not contiguous like vector
Slightly less cache-friendly
More flexible memory management

Flexibility:

Combines vector and queue
Best of both worlds
Use when need both capabilities
More versatile than vector or queue alone

return 0;

return 0; closes main and tells the operating system the program finished without an error.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

After push_back(1), push_front(2), push_back(3), what is the order?

Pick an answer to check it.

Question 2

Which operation is O(1) on a deque but O(n) on a vector?

Pick an answer to check it.

Questions people ask

Is deque always better than vector?

No. Vector is contiguous and faster to iterate. Use deque only when you need the front.

Does a deque have capacity() or reserve()?

No. It grows a chunk at a time.

Where is a deque used in algorithms?

Sliding window maximum and 0-1 BFS both rely on cheap pushes and pops at both ends.