Lesson 30 of 45
C++Lesson 30: Deque Container
9 min
Course progress67%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 30 of 45
C++9 min
Keep these ideas in mind as you work through the lesson.
std::deque (double‑ended queue) provides fast insertions and deletions at both the front and back while still supporting random access iterators.
vector.std::deque<int> dq;
// Push at the back
dq.push_back(1);
dq.push_back(2);
// Push at the front
dq.push_front(0);
// Pop from the front (FIFO)
int front = dq.front();
dq.pop_front();
std::cout << \"Popped \" << front << \", remaining:\";
for (int v : dq) std::cout << ' ' << v;
std::cout << '\
';
The deque grows in chunks, so it does not need to relocate all existing elements when expanding, unlike vector.
Tip: If you only need front‑or‑back operations,
std::vector(withpush_back) orstd::listmay be more appropriate depending on performance characteristics.
<Alert type="warning">std::deque does not guarantee a single contiguous memory block; pointer stability is only guaranteed for elements inserted at the front or back, not for random access via operator[] after reallocation.