🧩 C++ STL & Data Structures
Estimated reading: 3 minutes 277 views

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::deque is and how it works
  • Key operations like push_front(), push_back(), pop_front(), and more
  • How it compares to vector and list
  • 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

FunctionDescription
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

Featuredequevectorlist
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::deque allows 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 :
Share

C++ Deques

Or Copy Link

CONTENTS
Scroll to Top