Difference between revisions of "Template:cpp/container/erase"
(P1004R2) |
m (+inplace_vector() |
||
(19 intermediate revisions by 8 users not shown) | |||
Line 1: | Line 1: | ||
− | {{ | + | {{#vardefine:cont|{{{1|inplace_vector}}}}}<!-- |
− | {{cpp/container/{{ | + | -->{{cpp/container/{{#var:cont}}/title|erase}} |
+ | {{cpp/container/{{#var:cont}}/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
− | {{ | + | {{#switch:{{#var:cont}} |
− | + | |inplace_vector= | |
− | + | {{dcl|num=1|since=c++26| | |
− | + | ||
− | + | ||
− | + | ||
constexpr iterator erase( const_iterator pos ); | constexpr iterator erase( const_iterator pos ); | ||
}} | }} | ||
+ | {{dcl|num=2|since=c++26| | ||
+ | constexpr iterator erase( const_iterator first, const_iterator last ); | ||
}} | }} | ||
− | {{dcl rev multi | num=2 | + | | |
− | | dcl1= | + | {{dcl rev multi|num=1 |
+ | |dcl1= | ||
+ | iterator erase( iterator pos ); | ||
+ | |since2=c++11|notes2={{#ifeq:{{#var:cont}}|vector|{{mark constexpr since c++20}}}}|dcl2= | ||
+ | iterator erase( const_iterator pos ); | ||
+ | }} | ||
+ | {{dcl rev multi|num=2 | ||
+ | |dcl1= | ||
iterator erase( iterator first, iterator last ); | iterator erase( iterator first, iterator last ); | ||
− | | since2=c++11 | dcl2= | + | |since2=c++11|notes2={{#ifeq:{{#var:cont}}|vector|{{mark constexpr since c++20}}}}|dcl2= |
iterator erase( const_iterator first, const_iterator last ); | iterator erase( const_iterator first, const_iterator last ); | ||
− | |||
− | |||
}} | }} | ||
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
− | Erases the specified elements from the container. | + | Erases the specified elements from the container. |
− | @1@ Removes the element at {{ | + | @1@ Removes the element at {{c|pos}}. |
− | @2@ Removes the elements in the range {{ | + | @2@ Removes the elements in the range {{range|first|last}}. |
− | {{cpp/container/ | + | {{cpp/container/note iterator invalidation|{{#var:cont}}|erase}} |
− | The iterator {{ | + | The iterator {{c|pos}} must be valid and dereferenceable. Thus the {{lc|end()}} iterator (which is valid, but is not dereferenceable) cannot be used as a value for {{c|pos}}. |
− | The iterator {{ | + | The iterator {{c|first}} does not need to be dereferenceable if {{c|1=first == last}}: erasing an empty range is a no-op. |
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | pos | iterator to the element to remove}} | + | {{par|pos|iterator to the element to remove}} |
− | {{par | first, last | range of elements to remove}}<!-- | + | {{par|first, last|range of elements to remove}}<!-- |
− | -->{{#switch:{{ | + | -->{{#switch:{{#var:cont}} |
|vector|deque= | |vector|deque= | ||
{{par hreq}} | {{par hreq}} | ||
− | {{par req named | T | MoveAssignable}} | + | {{par req named|T|MoveAssignable}} |
}} | }} | ||
− | {{par end}} | + | {{par end}} |
===Return value=== | ===Return value=== | ||
Iterator following the last removed element. | Iterator following the last removed element. | ||
− | If {{ | + | @1@ If {{c|pos}} refers to the last element, then the {{lc|end()}} iterator is returned. |
− | If {{ | + | @2@ If {{c|1=last == end()}} prior to removal, then the updated {{lc|end()}} iterator is returned. |
− | If {{ | + | @@ If {{range|first|last}} is an empty range, then {{c|last}} is returned. |
===Exceptions=== | ===Exceptions=== | ||
− | {{#switch:{{ | + | {{#switch:{{#var:cont}} |
|list=(none) | |list=(none) | ||
− | |vector|deque= | + | |inplace_vector|vector|deque= |
Does not throw unless an exception is thrown by the assignment operator of {{tt|T}}. | Does not throw unless an exception is thrown by the assignment operator of {{tt|T}}. | ||
|{{templated}} | |{{templated}} | ||
Line 63: | Line 68: | ||
===Complexity=== | ===Complexity=== | ||
− | {{#switch:{{ | + | {{#switch:{{#var:cont}} |
− | | list= | + | |list= |
@1@ Constant. | @1@ Constant. | ||
− | @2@ Linear in the distance between {{ | + | @2@ Linear in the distance between {{c|first}} and {{c|last}}. |
− | | vector= | + | |inplace_vector|vector= |
− | Linear: the number of calls to the destructor of T is the same as the number of elements erased, the assignment operator of T is called the number of times equal to the number of elements in the vector after the erased elements | + | Linear: the number of calls to the destructor of {{tt|T}} is the same as the number of elements erased, the assignment operator of {{tt|T}} is called the number of times equal to the number of elements in the vector after the erased elements. |
− | | deque= | + | |deque= |
− | 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 | + | Linear: the number of calls to the destructor of {{tt|T}} is the same as the number of elements erased, the number of calls to the assignment operator of {{tt|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. |
− | | {{templated}} | + | |{{templated}} |
+ | }} | ||
+ | |||
+ | ===Notes=== | ||
+ | When container elements need to be erased based on a predicate, rather than iterating the container and calling unary {{tt|erase}}, the iterator range overload is generally used with {{ltt|cpp/algorithm/remove|std::remove()/std::remove_if()}} to minimise the number of moves of the remaining (non-removed) elements, — this is the erase-remove idiom. | ||
+ | {{#ifeq:{{#var:cont}}|inplace_vector | ||
+ | |{{rlpf|erase2|std::erase_if}} replaces the erase-remove idiom. | ||
+ | |{{rev inl|since=c++20|{{rlpf|erase2|std::erase_if}} replaces the erase-remove idiom.}} | ||
}} | }} | ||
===Example=== | ===Example=== | ||
+ | {{#switch:{{#var:cont}} | ||
+ | |inplace_vector= | ||
{{example | {{example | ||
− | + | |code= | |
− | #include <{{{1}}}> | + | #include <inplace_vector> |
+ | #include <print> | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | std::inplace_vector<int, 10> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; | ||
+ | std::println("{}", v); | ||
+ | |||
+ | v.erase(v.begin()); | ||
+ | std::println("{}", v); | ||
+ | |||
+ | v.erase(v.begin() + 2, v.begin() + 5); | ||
+ | std::println("{}", v); | ||
+ | |||
+ | // Erase all even numbers | ||
+ | for (std::inplace_vector<int, 10>::iterator it{v.begin()}; it != v.end();) | ||
+ | if (*it % 2 == 0) | ||
+ | it = v.erase(it); | ||
+ | else | ||
+ | ++it; | ||
+ | std::println("{}", v); | ||
+ | } | ||
+ | |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] | ||
+ | }} | ||
+ | | | ||
+ | {{example | ||
+ | |code= | ||
+ | #include <{{#var:cont}}> | ||
#include <iostream> | #include <iostream> | ||
− | {{#switch:{{ | + | {{#switch:{{#var:cont}} |
− | | vector | + | |vector|deque= |
− | | deque = | + | |
| | | | ||
#include <iterator> | #include <iterator> | ||
}} | }} | ||
− | + | void print_container(const std::{{#var:cont}}<int>& c) | |
{ | { | ||
− | + | for (int i : c) | |
− | for ( | + | std::cout << i << ' '; |
− | std::cout << i << | + | |
− | + | ||
std::cout << '\n'; | std::cout << '\n'; | ||
+ | } | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | std::{{#var:cont}}<int> c{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; | ||
+ | print_container(c); | ||
c.erase(c.begin()); | c.erase(c.begin()); | ||
+ | print_container(c); | ||
− | + | {{#switch:{{#var:cont}} | |
− | + | |vector | |
− | + | |deque= | |
− | + | c.erase(c.begin() + 2, c.begin() + 5); | |
− | + | ||
− | {{#switch:{{ | + | |
− | | vector | + | |
− | | deque = | + | |
− | c.erase(c.begin()+2, c.begin()+5); | + | |
| | | | ||
− | std::{{ | + | std::{{#var:cont}}<int>::iterator range_begin {{=}} c.begin(); |
− | std::{{ | + | std::{{#var:cont}}<int>::iterator range_end {{=}} c.begin(); |
− | std::advance(range_begin,2); | + | std::advance(range_begin, 2); |
− | std::advance(range_end,5); | + | std::advance(range_end, 5); |
c.erase(range_begin, range_end); | c.erase(range_begin, range_end); | ||
}} | }} | ||
+ | print_container(c); | ||
− | + | // Erase all even numbers | |
− | + | for (std::{{#var:cont}}<int>::iterator it = c.begin(); it != c.end();) | |
− | + | { | |
− | + | if (*it % 2 == 0) | |
− | + | ||
− | // Erase all even numbers | + | |
− | for ( | + | |
− | if (*it % 2 == 0) | + | |
it = c.erase(it); | it = c.erase(it); | ||
− | + | else | |
++it; | ++it; | ||
− | |||
} | } | ||
− | + | print_container(c); | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
} | } | ||
− | | output= | + | |output= |
0 1 2 3 4 5 6 7 8 9 | 0 1 2 3 4 5 6 7 8 9 | ||
1 2 3 4 5 6 7 8 9 | 1 2 3 4 5 6 7 8 9 | ||
Line 139: | Line 175: | ||
1 7 9 | 1 7 9 | ||
}} | }} | ||
− | + | }} | |
+ | <!----> | ||
+ | {{#ifeq:{{#var:cont}}|inplace_vector|<!--no DR-->| | ||
+ | ===Defect reports=== | ||
+ | {{dr list begin}} | ||
+ | {{dr list item|wg=lwg|dr=151|std=C++98|before={{c|first}} was required to be dereferenceable, which<br>made the behavior of clearing an empty {{tt|{{#var:cont}}}} undefined|after=not required if<br>{{c|1=first == last}}}} | ||
+ | {{#switch:{{#var:cont}}|vector= | ||
+ | {{dr list item|wg=lwg|dr=414|std=C++98|before=iterators at the point of erase were not invalidated|after=they are also invalidated}} | ||
+ | |deque= | ||
+ | {{dr list item|wg=lwg|dr=638|std=C++98|before=the past-the-end iterator was not invalidated|after=it is invalidated if the elements are<br>erased from the middle or the end}} | ||
+ | }} | ||
+ | {{dr list end}} | ||
+ | }} | ||
+ | <!----> | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/container/dsc | + | {{dsc inc|cpp/container/dsc erase seq|{{#var:cont}}}} |
+ | {{dsc inc|cpp/container/dsc clear|{{#var:cont}}}} | ||
{{dsc end}} | {{dsc end}} |
Latest revision as of 03:17, 17 August 2024
constexpr iterator erase( const_iterator pos ); |
(1) | (since C++26) |
constexpr iterator erase( const_iterator first, const_iterator last ); |
(2) | (since C++26) |
Erases the specified elements from the container.
[
first,
last)
.Iterators (including the end()
iterator) and references to the elements at or after the point of the erase are invalidated.
The iterator pos must be valid and dereferenceable. Thus 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.
Contents |
[edit] Parameters
pos | - | iterator to the element to remove |
first, last | - | range of elements to remove |
[edit] Return value
Iterator following the last removed element.
[
first,
last)
is an empty range, then last is returned.[edit] Exceptions
Does not throw unless an exception is thrown by the assignment operator of T
.
[edit] Complexity
Linear: the number of calls to the destructor of T
is the same as the number of elements erased, the assignment operator of T
is called the number of times equal to the number of elements in the vector after the erased elements.
[edit] 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.
[edit] Example
#include <inplace_vector> #include <print> int main() { std::inplace_vector<int, 10> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; std::println("{}", v); v.erase(v.begin()); std::println("{}", v); v.erase(v.begin() + 2, v.begin() + 5); std::println("{}", v); // Erase all even numbers for (std::inplace_vector<int, 10>::iterator it{v.begin()}; it != v.end();) if (*it % 2 == 0) it = v.erase(it); else ++it; std::println("{}", v); }
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]
[edit] See also
erases all elements satisfying specific criteria (function template) | |
clears the contents (public member function of std::inplace_vector<T,N> )
|