C++ deque erase() Function
Example
Remove the second element of a deque:
deque<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars.erase(cars.begin() + 1);
for (string car : cars) {
cout << car << "\n";
}
Definition and Usage
The erase() function removes one element, or a range of elements, from a deque. The elements to remove are given with iterators (objects that point to elements).
In the example, cars.begin() points to the first element, so cars.begin() + 1 points to the second element (BMW).
The erase function is defined in the <deque> header file.
Syntax
deque.erase(iterator position);
deque.erase(iterator start, iterator end);
Use the first form to remove one element, and the second form to remove a range of elements.
Parameter Values
| Parameter | Description |
|---|---|
| position | Required. An iterator pointing to the element to remove. |
| start | Required. An iterator pointing to the first element to remove. |
| end | Required. An iterator pointing to the position after the last element to remove. The element at this position is not removed. |
Technical Details
| Returns: | An iterator pointing to the element after the removed elements. If the removed elements were at the end, it is the same as deque.end(). |
|---|
Related Pages
Read more about deques in our Deque Tutorial.