๐ 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 :
