Difference between revisions of "Template:cpp/container/erase"
m (+inplace_vector() |
|||
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 ); | ||
+ | }} | ||
+ | {{dcl|num=2|since=c++26| | ||
+ | constexpr iterator erase( const_iterator first, const_iterator last ); | ||
+ | }} | ||
+ | | | ||
{{dcl rev multi|num=1 | {{dcl rev multi|num=1 | ||
|dcl1= | |dcl1= | ||
iterator erase( iterator pos ); | iterator erase( iterator pos ); | ||
− | |since2=c++11|notes2={{#ifeq:{{ | + | |since2=c++11|notes2={{#ifeq:{{#var:cont}}|vector|{{mark constexpr since c++20}}}}|dcl2= |
iterator erase( const_iterator pos ); | iterator erase( const_iterator pos ); | ||
}} | }} | ||
Line 11: | Line 21: | ||
|dcl1= | |dcl1= | ||
iterator erase( iterator first, iterator last ); | iterator erase( iterator first, iterator last ); | ||
− | |since2=c++11|notes2={{#ifeq:{{ | + | |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 {{c|pos}}. | @1@ Removes the element at {{c|pos}}. | ||
Line 22: | Line 33: | ||
@2@ Removes the elements in the range {{range|first|last}}. | @2@ Removes the elements in the range {{range|first|last}}. | ||
− | {{cpp/container/note iterator invalidation|{{ | + | {{cpp/container/note iterator invalidation|{{#var:cont}}|erase}} |
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 {{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}}. | ||
Line 32: | Line 43: | ||
{{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 | + | |vector|deque= |
− | |deque= | + | |
{{par hreq}} | {{par hreq}} | ||
{{par req named|T|MoveAssignable}} | {{par req named|T|MoveAssignable}} | ||
}} | }} | ||
− | {{par end}} | + | {{par end}} |
===Return value=== | ===Return value=== | ||
Line 50: | Line 60: | ||
===Exceptions=== | ===Exceptions=== | ||
− | {{#switch:{{ | + | {{#switch:{{#var:cont}} |
|list=(none) | |list=(none) | ||
− | |vector | + | |inplace_vector|vector|deque= |
− | |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 59: | Line 68: | ||
===Complexity=== | ===Complexity=== | ||
− | {{#switch:{{ | + | {{#switch:{{#var:cont}} |
|list= | |list= | ||
@1@ Constant. | @1@ Constant. | ||
@2@ Linear in the distance between {{c|first}} and {{c|last}}. | @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=== | ===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. {{rev inl|since=c++20|{{rlpf|erase2|std::erase_if}} replaces the erase-remove idiom.}} | + | 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= | |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::{{ | + | void print_container(const std::{{#var:cont}}<int>& c) |
{ | { | ||
for (int i : c) | for (int i : c) | ||
Line 94: | Line 139: | ||
int main() | int main() | ||
{ | { | ||
− | std::{{ | + | std::{{#var:cont}}<int> c{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
print_container(c); | print_container(c); | ||
− | + | ||
c.erase(c.begin()); | c.erase(c.begin()); | ||
print_container(c); | print_container(c); | ||
− | + | ||
− | {{#switch:{{ | + | {{#switch:{{#var:cont}} |
|vector | |vector | ||
− | |deque= | + | |deque= |
c.erase(c.begin() + 2, c.begin() + 5); | 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); | print_container(c); | ||
− | + | ||
// Erase all even numbers | // Erase all even numbers | ||
− | for (std::{{ | + | for (std::{{#var:cont}}<int>::iterator it = c.begin(); it != c.end();) |
{ | { | ||
if (*it % 2 == 0) | if (*it % 2 == 0) | ||
Line 130: | Line 175: | ||
1 7 9 | 1 7 9 | ||
}} | }} | ||
− | + | }} | |
+ | <!----> | ||
+ | {{#ifeq:{{#var:cont}}|inplace_vector|<!--no DR-->| | ||
===Defect reports=== | ===Defect reports=== | ||
{{dr list begin}} | {{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|{{ | + | {{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:{{ | + | {{#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}} | {{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= | |deque= | ||
Line 140: | Line 187: | ||
}} | }} | ||
{{dr list end}} | {{dr list end}} | ||
− | + | }} | |
+ | <!----> | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc|cpp/container/dsc erase seq|{{ | + | {{dsc inc|cpp/container/dsc erase seq|{{#var:cont}}}} |
− | {{dsc inc|cpp/container/dsc clear|{{ | + | {{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> )
|