std::deque<T> · C++

pop_front / pop_back

Removes an element at either end (read front()/back() first: they return nothing).

void pop_front(); void pop_back();

Complexity

Best caseO(1)
Worst caseO(1)
Extra spaceO(1)

Destroy the end element; free its block if empty.

Tested example

std::deque<int> d{1, 2, 3};
assert(d.front() == 1);
d.pop_front();  // returns nothing: read front() first
d.pop_back();
assert(d.size() == 1 && d.front() == 2);
// pop_front() on an empty deque is undefined behaviour: check empty() first
d.pop_front();
assert(d.empty());
Runs in the test suite on C++23, GCC 15.3 (libstdc++) · examples/cpp/collections/deque_examples.cpp

Behaviour

Returns
Nothing.
Errors and preconditions
Undefined behaviour on an empty deque.
Changes
size() decreases by 1.
Memory and invalidation
Invalidates only iterators and references to the removed element (and end() for pop_back).