Section

std::deque

std::deque (double-ended queue) is an indexed sequence container that supports fast insertion and removal at both ends.

Unlike std::vector, a deque does not store its elements in one single contiguous block. It still offers random-access indexing, but its segmented storage changes the iterator-invalidation and memory-locality tradeoffs.

# Declarations

template<
class T,
class Allocator = std::allocator<T>
> class deque;
namespace pmr {
template< class T >
using deque = std::deque<T, std::pmr::polymorphic_allocator<T>>;
}

(since C++17)

# Example

#include <deque>
#include <iostream>
 
int main()
{
    // Create a deque containing integers
    std::deque<int> d = {7, 5, 16, 8};
 
    // Add an integer to the beginning and end of the deque
    d.push_front(13);
    d.push_back(25);
 
    // Iterate and print values of deque
    for (int n : d)
        std::cout << n << ' ';
    std::cout << '\n';
}

deque is a good fit when the program needs cheap growth at both the front and the back while keeping indexed access.

# Template parameters

  • T: element type. The exact requirements depend on the operations used. In general, elements must be erasable, and many operations impose stronger construction, assignment, or move requirements.
  • Allocator: allocator used to obtain storage and manage element lifetimes. It must satisfy the Allocator requirements. Since C++20, the program is ill-formed if Allocator::value_type is not T.

# Member types

Member typeDefinition
value_typeT
allocator_typeAllocator
size_typeUnsigned integer type, usually std::size_t
difference_typeSigned integer type, usually std::ptrdiff_t
referencevalue_type&
const_referenceconst value_type&
pointerallocator-aware pointer type to value_type
const_pointerallocator-aware pointer type to const value_type
iteratorRandom-access iterator to value_type
const_iteratorRandom-access iterator to const value_type
reverse_iteratorstd::reverse_iterator<iterator>
const_reverse_iteratorstd::reverse_iterator<const_iterator>

# Iterator invalidation

OperationInvalidation
Read-only operationsNever
swap, std::swapThe past-the-end iterator may be invalidated
shrink_to_fit, clear, insert, emplace, push_front, push_back, emplace_front, emplace_backAll iterators are invalidated
eraseErased elements are invalidated; erasing in the middle invalidates all iterators, while erasing at an end is more limited
resizeGrowing invalidates all iterators; shrinking invalidates erased elements and the past-the-end iterator
pop_front, pop_backThe erased element is invalidated; the past-the-end iterator is also invalidated since C++11

# Invalidation notes

  • Insertions at either end do not invalidate references to existing elements.
  • Erasing at either end does not invalidate references to non-erased elements.
  • Resizing larger invalidates iterators but does not invalidate references to existing elements.
  • Resizing smaller preserves references to non-erased elements.

# Reference map

AreaKey entries
Construction and lifetimedeque::deque, deque::~deque, deque::operator=, assign, assign_range, get_allocator
Element accessat, operator[], front, back
Iterators and capacitybegin, end, rbegin, rend, empty, size, max_size, shrink_to_fit
Modifiersclear, insert, insert_range, emplace, erase, push_back, emplace_back, append_range, pop_back, push_front, emplace_front, prepend_range, pop_front, resize, swap
Non-member functionscomparison operators, std::swap(std::deque), erase, erase_if
Type deductiondeduction guides

# Notes

deque offers constant-time insertion and removal at both ends, but it does not provide the same contiguous-storage guarantee as vector.

That makes deque attractive for front/back growth patterns, but less suitable for APIs that require a pointer to one contiguous array buffer.

# Feature-test macros

MacroValueStdFeature
__cpp_lib_containers_ranges202202LC++23ranges construction and insertion for containers

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 230C++98T was not required to be CopyConstructibleT is also required to be CopyConstructible

# See also