C++ Deque
std::deque (pronounced "deck") stands for double-ended queue. It combines features of vector and list: like a vector it supports fast access by index, and like a list it supports fast insertion and removal at both ends. It lives in the <deque> header.
How it differs from vector
A vector keeps all elements in one contiguous block, so adding at the front means shifting everything. A deque stores elements in a series of fixed-size chunks and keeps a small index of those chunks. Adding at either end just fills (or creates) a chunk at that end.
| Operation | vector |
deque |
list |
|---|---|---|---|
push_back / pop_back |
fast | fast | fast |
push_front / pop_front |
not available | fast | fast |
Index access [i] |
fast | fast | not available |
| Insert in the middle | slow | slow | fast (with iterator) |
| One contiguous memory block | yes | no | no |
Basic usage
#include <iostream>
#include <deque>
using namespace std;
void print(const deque<int>& d) {
for (int x : d) cout << x << " ";
cout << "\n";
}
int main() {
deque<int> d = {3, 4, 5};
d.push_front(2);
d.push_front(1);
d.push_back(6);
print(d);
cout << "d[2] = " << d[2] << ", front = " << d.front() << ", back = " << d.back() << "\n";
d.pop_front();
d.pop_back();
print(d);
cout << "size: " << d.size() << "\n";
}1 2 3 4 5 6
d[2] = 3, front = 1, back = 6
2 3 4 5
size: 4
Deques support almost everything vectors do: at(), insert, erase, iterators, range-based loops, and all the standard algorithms such as sort.
Example: a sliding window
A deque is the ideal structure for a "most recent N items" window: push new items at the back, drop old ones from the front. Here we compute a moving average over the last 3 readings:
#include <iostream>
#include <deque>
#include <iomanip>
using namespace std;
int main() {
double readings[] = {10, 12, 11, 15, 20, 18, 14};
deque<double> window;
const size_t N = 3;
double sum = 0;
cout << fixed << setprecision(2);
for (double r : readings) {
window.push_back(r);
sum += r;
if (window.size() > N) {
sum -= window.front();
window.pop_front();
}
cout << "reading " << r << " -> avg " << sum / window.size() << "\n";
}
}reading 10.00 -> avg 10.00
reading 12.00 -> avg 11.00
reading 11.00 -> avg 11.00
reading 15.00 -> avg 12.67
reading 20.00 -> avg 15.33
reading 18.00 -> avg 17.67
reading 14.00 -> avg 17.33
Example: palindrome check
Because a deque gives fast access to both ends, checking whether a word reads the same forwards and backwards is natural — compare and remove from both ends until one or zero characters remain:
#include <iostream>
#include <deque>
#include <string>
#include <cctype>
using namespace std;
bool isPalindrome(const string& s) {
deque<char> d;
for (char c : s) {
if (isalpha(static_cast<unsigned char>(c))) d.push_back(tolower(c));
}
while (d.size() > 1) {
if (d.front() != d.back()) return false;
d.pop_front();
d.pop_back();
}
return true;
}
int main() {
for (string s : {"Racecar", "Never odd or even", "deque"}) {
cout << '"' << s << "\" -> " << boolalpha << isPalindrome(s) << "\n";
}
}"Racecar" -> true
"Never odd or even" -> true
"deque" -> false
Example: a simple card deck
#include <iostream>
#include <deque>
#include <string>
using namespace std;
int main() {
deque<string> cards = {"7H", "QS", "2D", "AC", "10C"};
string drawnTop = cards.front(); cards.pop_front();
string drawnBottom = cards.back(); cards.pop_back();
cards.push_front("KH"); // put a card on top
cout << "drew " << drawnTop << " and " << drawnBottom << "\n";
cout << "deck now:";
for (size_t i = 0; i < cards.size(); i++) cout << " " << cards[i];
cout << "\n";
}drew 7H and 10C
deck now: KH QS 2D AC
When to choose a deque
- You need to add or remove at both ends and also want index access.
- You are building a queue — in fact
std::queueandstd::stackusedequeinternally by default. - The collection is very large and you want to avoid the big reallocations a vector performs when it grows.
Note: Because a deque is not one contiguous block, you cannot pass
d.data()to C functions that expect a plain array — deque has nodata()member. Use a vector for that.
Test yourself
What does "deque" stand for?
- Dynamic queue
- Double-ended queue
- Default queue
- Decreasing queue
Which operation does deque support that vector does not?
- push_back
- operator[]
- push_front
- size
Which container does std::stack use by default under the hood?
- vector
- list
- deque
- array