std::deque::erase(3)
β¨ NAME β¨
std::deque::erase β literally std::deque::erase, bestie ππ
. This page is from the C++ Standard Libary manual, and we are about to erase elements with main-character energy.
πΈ Synopsis πΈ
Hereβs the synopsis as the page lays it out β the wrapping is a little chaotic, but the tokens are the tokens π«Ά:
iterator erase( iterator pos ); (until
C++11)
iterator erase( const_iterator pos ); (since
C++11)
iterator erase( iterator first, iterator last ); (1)
(until C++11)
iterator erase( const_iterator first, (2)
(since C++11)
const_iterator last );
Erases the specified elements from the container. π
- Removes the element at
pos. - Removes the elements in the range
[first, last).
All iterators and references are invalidated, unless the erased elements are at the end or at the beginning of the container, in which case only the iterators and references to the erased elements are invalidated. The end() iterator is also invalidated unless the erased elements are at the beginning of the container and the last element is not erased. π
The iterator pos must be valid and dereferenceable. So the end() iterator β which is valid, but is not dereferenceable β cannot be used as a value for pos. π¨
The iterator first does not need to be dereferenceable if first == last: erasing an empty range is a no-op. β¨
π Parameters π
posβ iterator to the element to removefirst, lastβ range of elements to remove
π¦ Type requirements π¦
Tmust meet the requirements ofMoveAssignable.
π Return value π
Iterator following the last removed element. π
- If
posrefers to the last element, then theend()iterator is returned. - If
last == end()prior to removal, then the updatedend()iterator is returned. If[first, last)is an empty range, thenlastis returned.
π¨ Exceptions π¨
Does not throw unless an exception is thrown by the assignment operator of T.
π Complexity π
Linear: the number of calls to the destructor of T is the same as the number of elements erased, the number of calls to the assignment operator of T is no more than the lesser of the number of elements before the erased elements and the number of elements after the erased elements. π§
π Notes π
When container elements need to be erased based on a predicate, rather than iterating the container and calling unary erase, the iterator range overload is generally used with std::remove()/std::remove_if() to minimise the number of moves of the remaining (non-removed) elements, this is the erase-remove idiom. πΈ
std::erase_if() replaces the erase-remove idiom.
(since C++20)
π Example π
// Run this code
#include <deque>
#include <iostream>
void print_container(const std::deque<int>& c)
{
for (int i : c)
std::cout << i << ' ';
std::cout << '\n';
}
int main()
{
std::deque<int> c{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
print_container(c);
c.erase(c.begin());
print_container(c);
c.erase(c.begin() + 2, c.begin() + 5);
print_container(c);
// Erase all even numbers
for (std::deque<int>::iterator it = c.begin(); it != c.end();)
{
if (*it % 2 == 0)
it = c.erase(it);
else
++it;
}
print_container(c);
}
π Output π
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 6 7 8 9
1 7 9
π₯ Defect reports π₯
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. π
- DR:
LWG 151Β· Applied to:C++98Β· Behavior as published:firstwas required to be dereferenceable, which made the behavior of clearing an emptydequeundefined Β· Correct behavior: not required iffirst == last - DR:
LWG 638Β· Applied to:C++98Β· Behavior as published: the past-the-end iterator was not invalidated Β· Correct behavior: it is invalidated if the elements are erased from the middle or the end
π See also π
erase(std::deque)β erases all elements satisfying specific criteriaerase_if(std::deque)β(function template)(C++20)clearβ clears the contents(public member function)
okay thatβs the whole page, bestie β source: http://cppreference.com, version/date: 2024.06.10, page: std::deque::erase(3) πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms