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.
1#include <iostream>2#include <deque>3using namespace std;45int main() {6 // Create deque7 deque<int> dq;89 // Add elements at both ends10 dq.push_back(10);11 dq.push_back(20);12 dq.push_back(30);13 dq.push_front(5);14 dq.push_front(1);1516 cout << "Deque elements: ";17 for (int num : dq) {18 cout << num << " ";19 }20 cout << endl;2122 cout << "Front: " << dq.front() << endl;23 cout << "Back: " << dq.back() << endl;24 cout << "Size: " << dq.size() << endl;2526 // Access elements by index27 cout << "Element at index 2: " << dq[2] << endl;28 cout << "Element at index 2 (at): " << dq.at(2) << endl;2930 // Remove from front31 dq.pop_front();32 cout << "\nAfter pop_front: ";33 for (int num : dq) {34 cout << num << " ";35 }36 cout << endl;3738 // Remove from back39 dq.pop_back();40 cout << "After pop_back: ";41 for (int num : dq) {42 cout << num << " ";43 }44 cout << endl;4546 // Insert at specific position47 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;5354 // Erase element55 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;6162 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
push_back 10, 20, 30; push_front 5, then 1.
- 2
Print 1 5 10 20 30 with front, back, and size.
- 3
Read index 2 with [] and at(): 10.
- 4
pop_front → 5 10 20 30; pop_back → 5 10 20.
- 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:
Key Features:
Adding Elements
At Back:
dq.push_back(10); // Add to end
At Front:
dq.push_front(5); // Add to beginning
What happens here:
Removing Elements
From Back:
dq.pop_back(); // Remove from end
From Front:
dq.pop_front(); // Remove from beginning
What happens here:
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:
Inserting at Position
Using insert():
dq.insert(dq.begin() + 1, 15);
What happens here:
When to Use Deque
Good fits:
Example Scenarios:
Important Considerations
Performance:
Memory Layout:
Flexibility:
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.