← Back to C++ Course | Chapter 13: STL Containers & Algorithms | Lesson 4 of 15

C++ deque

A deque is a list where you can quickly add or remove items at both ends, like a line where people can join or leave at the front or back. It is short for double-ended queue.
Syntax
cpp
#include <deque>

std::deque<data_type> deque_name = {value1, value2};
deque_name.push_front(value);
deque_name.push_back(value);

What is a deque?

A deque, short for double-ended queue, is a sequence container that behaves like a hybrid of a vector and a linked list: it offers fast insertion and removal at both ends while still supporting direct random-access indexing like a vector does.

Example: What is a deque?

cpp
// Include std::cout and std::cin
#include <iostream>
#include <deque>

// Program execution starts in main()
int main() {
	std::deque<int> d = {1, 2, 3};
	// Print to the console with cout
	std::cout << d[0] << " " << d.front() << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Fast Inserts at Front and Back

Unlike a vector, a deque supports adding elements to its front in constant time using push_front(), in addition to the push_back() you'd already expect — a capability vectors simply don't offer efficiently.

Example: Fast Inserts at Front and Back

cpp
// Include std::cout and std::cin
#include <iostream>
#include <deque>

// Program execution starts in main()
int main() {
	std::deque<int> d = {2, 3};
	d.push_front(1);
	d.push_back(4);
	for (int x : d) std::cout << x << " ";
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Accessing Elements in Deque

Because a deque supports random access, you can reach any element directly through index brackets like d[i], or through the bounds-checked at() method, exactly the same way you would with a vector.

Example: Accessing Elements in Deque

cpp
// Include std::cout and std::cin
#include <iostream>
#include <deque>

// Program execution starts in main()
int main() {
	std::deque<int> d = {10, 20, 30};
	// Print to the console with cout
	std::cout << d[1] << " " << d.at(2) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Removing Elements from Deque

You can remove elements from either end of a deque using pop_front() or pop_back(), and both operations automatically update the container's reported size, mirroring how push and pop work at the opposite end.

Example: Removing Elements from Deque

cpp
// Include std::cout and std::cin
#include <iostream>
#include <deque>

// Program execution starts in main()
int main() {
	std::deque<int> d = {1, 2, 3};
	d.pop_front();
	d.pop_back();
	// Print to the console with cout
	std::cout << d.size() << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Iterating over Deque

Just like a vector, a deque can be traversed with an index-based loop, a range-based for loop, or STL iterators — the uniform iteration interface is one of the main benefits of building on the STL's shared container conventions.

Example: Iterating over Deque

cpp
// Include std::cout and std::cin
#include <iostream>
#include <deque>

// Program execution starts in main()
int main() {
	std::deque<int> d = {1, 2, 3};
	// Loop: repeat while the condition holds
	for (int i = 0; i < d.size(); i++) std::cout << d[i] << " ";
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using [] past the end of a deque, which is undefined behavior.
  2. Assuming a deque stores elements in one contiguous block like a vector.
  3. Erasing elements in a loop and continuing with an invalidated iterator.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.