C++ std::deque Functions

About This Index
This page is the C++ std::deque Functions index. Each row links to a full guide with syntax, parameters, return value, three examples, and FAQs.
Learn what a std::deque is on /cpp/deque. Use this page when you need a specific member function such as push_front(), pop_back(), or at().
A std::deque<T> can add and remove at the front and the back quickly, and it also allows d[i] by index. It has no capacity() or reserve(). The 31 functions are listed below.
How to Call a Deque Function
Include <deque>, create the deque, and call members with a dot:
#include <deque>
#include <iostream>
std::deque<int> d = {20, 30};
d.push_front(10); // 10 20 30
d.push_back(40); // 10 20 30 40
int first = d.front(); // 10
int second = d.at(1); // 20, checked
d.pop_back(); // 10 20 30
std::size_t n = d.size(); // 3 C++ std::deque Functions Table
All function guides in this C++ tutorial series (31 tutorials), grouped by purpose.
Element access
3 functionsRead or change elements by position.
Capacity
5 functionsAsk about the size, or change it. A deque has no capacity() or reserve().
| Function | Description | Tutorial |
|---|---|---|
empty() | Check whether the deque has no elements. | Try it |
size() | Get the number of elements in the deque. | Try it |
max_size() | Get the largest number of elements a deque could ever hold. | Try it |
resize() | Change the number of elements, adding or removing at the back. | Try it |
shrink_to_fit() | Ask the deque to release unused memory (C++11, non-binding). | Try it |
Modifiers
14 functionsAdd, remove, or replace elements. Both ends are fast.
| Function | Description | Tutorial |
|---|---|---|
assign() | Replace all elements with new ones. | Try it |
assign_range() | Replace all elements with the items of a range (C++23). | Try it |
clear() | Remove every element from the deque. | Try it |
insert() | Add one or more elements at any position. | Try it |
insert_range() | Insert a whole range at any position (C++23). | Try it |
erase() | Remove one element or a range of elements. | Try it |
push_back() | Add an element at the back. | Try it |
push_front() | Add an element at the front. | Try it |
pop_back() | Remove the last element. | Try it |
pop_front() | Remove the first element. | Try it |
emplace() | Build a new element in place at any position (C++11). | Try it |
emplace_back() | Build a new element in place at the back (C++11). | Try it |
emplace_front() | Build a new element in place at the front (C++11). | Try it |
swap() | Exchange the content of two deques. | Try it |
Iterators
8 functionsWalk through the deque forwards or backwards. The c versions are read-only.
| Function | Description | Tutorial |
|---|---|---|
begin() | Get an iterator to the first element. | Try it |
end() | Get an iterator one place after the last element. | Try it |
cbegin() | Get a read-only iterator to the first element. | Try it |
cend() | Get a read-only iterator one place after the last element. | Try it |
rbegin() | Get a reverse iterator to the last element. | Try it |
rend() | Get the reverse end marker, just before the first element. | Try it |
crbegin() | Get a read-only reverse iterator to the last element. | Try it |
crend() | Get a read-only reverse end marker, just before the first element. | Try it |
Allocator
1 functionAccess the memory helper object used by the deque.
| Function | Description | Tutorial |
|---|---|---|
get_allocator() | Get a copy of the allocator the deque uses. | Try it |
Frequently Asked Questions
Start with push_front()
Add an element at the front, the one thing a vector cannot do quickly.
12 people found this page helpful
