Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Lesson 30: Deque Container | C++ Course lesson | BraveProgrammer
  1. Courses
  2. /
  3. C++ Course

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.

4 points
  • Random access iterator support
  • Differences from vector
  • Use cases (e.g., sliding windows)
  • Memory layout characteristics

Deque Container

std::deque (double‑ended queue) provides fast insertions and deletions at both the front and back while still supporting random access iterators.


When to use

  • Implementing BFS queues, sliding windows, or buffers where both ends are manipulated frequently.
  • Need for contiguous blocks of memory for cache friendliness is less important than for vector.

Example: Simulating a simple queue with a deque

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 (with push_back) or std::list may 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.

Make it stickPractice what you've learned1 quiz · 2 practical tests · Optional activities from C++ CourseBrowse activities
Previous lessonLesson 29: List ContainerContinue to lesson 31Lesson 31: Set and Unordered SetBack to C++ Course curriculum