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 case | O(1) |
|---|---|
| Worst case | O(1) |
| Extra space | O(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());
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).