C++ Deque

Anonymous contributor's avatar
Anonymous contributor
Published Sep 25, 2024
Contribute to Docs

A deque (double-ended queue) in C++ is a sequence container that allows adding and removing elements from both the front and back efficiently. Unlike a vector, which only allows fast insertions and deletions at the back, a deque provides constant time complexity for insertions and deletions at both ends. This is useful for scenarios like implementing queues, stacks, or sliding window algorithms.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours

Syntax

std::deque<datatype> name;
  • datatype: The type of elements the deque will hold (e.g., int, float, string, etc.).
  • name: The name of the deque.

Example

The following code inserts elements (13 & 8) at the front and back and access them.

#include <deque>
#include <iostream>
int main() {
std::deque<int> myDeque; // Declare a deque of integers
myDeque.push_back(13); // Insert element at the back
myDeque.push_front(8); // Insert element at the front
std::cout << "Front: " << myDeque.front() << std::endl;
std::cout << "Back: " << myDeque.back() << std::endl;
return 0;
}

The output of the above program will be:

Front: 8
Back: 13

Equivalent Methods in Stack, Queue, and Deque

Operation Stack Queue Deque
Insert push() push() (back) push_back(), push_front()
Remove pop() (top) pop() (front) pop_back(), pop_front()
Access top() front(), back() front(), back()
Check Empty empty() empty() empty()

Deque

.back()
Returns a reference to the last element in the deque.
.clear()
Removes all elements from a given deque.
.empty()
Checks if a given deque container is empty.
.front()
Returns a reference to the first element of the deque
.get_allocator()
Returns a copy of the allocator that is assigned to deque.
.insert()
Inserts an element at a specified position in the deque.
.max_size()
Returns the maximum number of elements a deque can hold.
.pop_back()
Removes the last element from a deque.
.push_back()
Adds an element to the end of the deque.
.push_front()
Adds an element to the beginning of the deque.
.rend()
Returns a reverse iterator pointing to the element before the first element in the deque container.
.shrink_to_fit()
Requests the deque to reduce its capacity to match its size.
.swap()
Exchanges the contents of one deque with another of the same type and size.
pop_front()
Removes the first element from a deque container in C++.

All contributors

Contribute to Docs

Learn C++ on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours