In C++, std::deque::erase() removes elements from a std::deque. You can remove one element by giving its iterator, or a range by giving two iterators.
It returns an iterator to the element that comes right after the removed ones. Include <deque> to use it.
This is a member of std::deque<T>, a double-ended queue. A deque is fast at adding and removing at both ends, and it allows access by index like std::vector. Unlike a vector, its elements are stored in several blocks, not in one contiguous block.
Syntax
Pass one iterator to remove one element, or two iterators to remove a range.
C++
d.erase(d.begin() + 1); // remove index 1
d.erase(d.begin() + 1, d.begin() + 3); // remove indexes 1 and 2
Call
What it does
d.erase(pos)
Removes the element that pos points to.
d.erase(first, last)
Removes every element from first up to, but not including, last.
To remove only the first or last element, pop_front() and pop_back() are simpler and faster.
Required for the first form. An iterator to the element to remove. It must not be end().
first, last
Required for the second form. The range [first, last) to remove. last is the first element that is kept.
Return Value
Return Value
Description
Iterator
An iterator to the element after the last removed one. If the removed element was at the back, it is end().
C++ Examples
Example 1 — Erase One Element
Remove the second element by its iterator.
C++
#include <deque>
#include <iostream>
int main() {
std::deque<int> d = {10, 20, 30, 40};
d.erase(d.begin() + 1);
for (int n : d) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
10 30 40
How It Works
d.begin() + 1 is an iterator to the element at index 1, which is 20.
erase() removes that element, and 30 and 40 shift one place towards the front.
The size drops from 4 to 3, and the remaining order is 10 30 40.
Example 2 — Erase a Range
Give two iterators to remove several elements in one call.
C++
#include <deque>
#include <iostream>
int main() {
std::deque<int> d = {1, 2, 3, 4, 5, 6};
d.erase(d.begin() + 1, d.begin() + 4);
for (int n : d) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
1 5 6
How It Works
The range starts at index 1 (value 2) and stops before index 4 (value 5).
So the elements 2 3 4 are removed. The element 5 is not removed, because last is excluded.
What remains is 1 from the start and 5 6 from the end.
Example 3 — Erase While Looping
Use the returned iterator to remove all even numbers safely.
C++
#include <deque>
#include <iostream>
int main() {
std::deque<int> d = {1, 2, 3, 4, 5, 6};
for (auto it = d.begin(); it != d.end();) {
if (*it % 2 == 0) {
it = d.erase(it);
} else {
++it;
}
}
for (int n : d) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
1 3 5
How It Works
The loop has no ++it in its header, because the iterator only moves when nothing is erased.
For an even number, it = d.erase(it) removes it and gives back an iterator to the next element, so no element is skipped.
For an odd number the code calls ++it. At the end only the odd numbers 1 3 5 remain.
Frequently Asked Questions
It removes one element, or a range of elements, from the deque and returns an iterator to the element after the removed ones.
Use d.erase(d.begin() + i). Deque iterators are random-access, so adding i to begin() works.
Erasing in the middle invalidates the old iterators. The returned iterator is valid and points to the next element, so you can continue your loop safely.
Only near the two ends. In the middle, the elements on one side must move, so the time grows with the size of the deque.
Use the erase-remove idiom: d.erase(std::remove(d.begin(), d.end(), value), d.end()); In C++20 you can write std::erase(d, value) instead.