C++ Deques β Double-Ended Queues with std::deque
Introduction β Why Use std::deque in C++
In scenarios where you need fast insertion and deletion from both ends of a sequence, std::deque (double-ended queue) is the go-to STL container. It offers the combined strengths of both vector and list, making it ideal for queue-like behavior with random access capabilities.
In this guide, youβll learn:
- What
std::dequeis and how it works - Key operations like
push_front(),push_back(),pop_front(), and more - How it compares to
vectorandlist - Use cases and performance tips
What Is a C++ Deque?
A std::deque is a sequence container that allows constant time insertion and deletion from both the front and back. Unlike vector, it doesn’t guarantee contiguous storage but still provides random access.
Include the header:
#include <deque>
Declare a deque:
deque<int> dq;
Code Examples β With Output
Basic Operations
#include <iostream>
#include <deque>
using namespace std;
int main() {
deque<int> dq;
dq.push_back(10);
dq.push_front(5);
dq.push_back(15);
for (int x : dq) cout << x << " ";
return 0;
}
Output:
5 10 15
deque Member Functions
| Function | Description |
|---|---|
push_back() | Insert at the back |
push_front() | Insert at the front |
pop_back() | Remove from the back |
pop_front() | Remove from the front |
at(index) | Access with bounds checking |
[] | Access without bounds checking |
front() | Get first element |
back() | Get last element |
size() | Number of elements |
clear() | Remove all elements |
empty() | Check if empty |
deque vs vector vs list
| Feature | deque | vector | list |
|---|---|---|---|
| Random access | Yes | Yes | No |
| Insert/remove ends | Fast | Back only | Front/back |
| Insert/remove middle | Slow | Slow | Fast |
| Contiguous memory | No | Yes | No |
Best Practices & Tips
Use deque when you need fast insertions/removals at both ends and random access
Avoid middle insertionsβuse list if frequent
Unlike vector, deque does not guarantee contiguous storageβdo not assume pointer arithmetic
When building queues, stacks, or sliding windows, deque is a perfect fit
Use Cases for Deques
Sliding Window Algorithms β Efficient front and back updates
Double-Ended Queues β Task schedulers, real-time buffers
Expression Evaluators β Stack/queue hybrid logic
Input Buffers β Read from front, write to back
Palindrome Checkers β Compare characters from both ends
Summary β Recap & Next Steps
Key Takeaways:
std::dequeallows fast insertion/removal from both front and back- Supports random access like
vector - Ideal for use cases where data grows/shrinks at both ends
Real-World Relevance:
C++ deques are used in scheduling systems, real-time input processors, algorithm optimization, and double-ended data buffers in simulations and stream-based applications.
Next Steps:
Explore C++ Sets to work with sorted, unique collections using std::set.
FAQ β C++ Deques
Is deque faster than vector?
Depends. deque is faster for front insertions; vector is faster for contiguous data operations.
Can I use STL algorithms with deque?
Yes. deque supports full iterator support.
Does deque support random access?
Yes, via at(index) and [].
Is deque memory contiguous?
No. Internally segmented; donβt assume raw pointer access like arrays.
Should I use deque for queue and stack?
Yes. Itβs the default underlying container for both std::queue and std::stack.
Share Now :
