Namespaces
Variants
Views
Actions

Difference between revisions of "Template:cpp/container/erase"

From cppreference.com
m (be explicit instead of using unnecessary auto, which obfuscates)
 
(16 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{cpp/container/{{{1|}}}/title | erase}}
+
{{#vardefine:cont|{{{1|inplace_vector}}}}}<!--
{{cpp/container/{{{1|}}}/navbar}}
+
-->{{cpp/container/{{#var:cont}}/title|erase}}
 +
{{cpp/container/{{#var:cont}}/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl rev multi | num=1
+
{{#switch:{{#var:cont}}
| dcl1=
+
|inplace_vector=
iterator erase( iterator pos );
+
{{dcl|num=1|since=c++26|
| since2=c++11 | dcl2=
+
iterator erase( const_iterator pos );
+
| since3={{#ifeq:{{{1}}}|vector|c++20}} | dcl3={{#ifeq:{{{1}}}|vector|
+
 
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 );
| since3={{#ifeq:{{{1}}}|vector|c++20}} | dcl3={{#ifeq:{{{1}}}|vector|
 
constexpr 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 {{tt|pos}}.
+
@1@ Removes the element at {{c|pos}}.
  
@2@ Removes the elements in the range {{tt|[first, last)}}.
+
@2@ Removes the elements in the range {{range|first|last}}.
  
{{cpp/container/note_iterator_invalidation|{{{1|}}}|erase}}
+
{{cpp/container/note iterator invalidation|{{#var:cont}}|erase}}
  
The iterator {{tt|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 {{tt|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}}.
  
The iterator {{tt|first}} does not need to be dereferenceable if {{tt|first{{==}}last}}: erasing an empty range is a no-op.
+
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:{{{1}}}
+
-->{{#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 {{tt|pos}} refers to the last element, then the {{lc|end()}} iterator is returned.
+
@1@ If {{c|pos}} refers to the last element, then the {{lc|end()}} iterator is returned.
  
If {{tt|last{{==}}end()}} prior to removal, then the updated {{lc|end()}} iterator is returned.
+
@2@ If {{c|1=last == end()}} prior to removal, then the updated {{lc|end()}} iterator is returned.
  
If {{tt|[first, last)}} is an empty range, then {{tt|last}} is returned.
+
@@ If {{range|first|last}} is an empty range, then {{c|last}} is returned.
  
 
===Exceptions===
 
===Exceptions===
{{#switch:{{{1|}}}
+
{{#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:{{{1|}}}  
+
{{#switch:{{#var:cont}}
| list=
+
|list=
 
@1@ Constant.
 
@1@ Constant.
@2@ Linear in the distance between {{tt|first}} and {{tt|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===
 +
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, &mdash; 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:{{{1}}}
+
{{#switch:{{#var:cont}}
| vector
+
|vector|deque=
| deque =  
+
 
|
 
|
 
#include <iterator>
 
#include <iterator>
 
}}
 
}}
  
void print_container(const std::{{{1}}}<int>& c)  
+
void print_container(const std::{{#var:cont}}<int>& c)
 
{
 
{
     for (int i : c) {
+
     for (int i : c)
         std::cout << i << " ";
+
         std::cout << i << ' ';
    }
+
 
     std::cout << '\n';
 
     std::cout << '\n';
 
}
 
}
  
int main( )
+
int main()
 
{
 
{
     std::{{{1}}}<int> c{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
+
     std::{{#var:cont}}<int> c{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
 
     print_container(c);
 
     print_container(c);
  
Line 102: Line 145:
 
     print_container(c);
 
     print_container(c);
  
     {{#switch:{{{1}}}
+
     {{#switch:{{#var:cont}}
| vector
+
|vector
| deque =  
+
|deque=
     c.erase(c.begin()+2, c.begin()+5);
+
     c.erase(c.begin() + 2, c.begin() + 5);
 
|
 
|
     std::{{{1}}}<int>::iterator range_begin {{=}} c.begin();
+
     std::{{#var:cont}}<int>::iterator range_begin {{=}} c.begin();
     std::{{{1}}}<int>::iterator range_end {{=}} c.begin();
+
     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);
Line 116: Line 159:
 
     print_container(c);
 
     print_container(c);
  
     // Erase all even numbers (C++11 and later)
+
     // Erase all even numbers
     for (std::{{{1}}}<int>::iterator it = c.begin(); it != c.end(); ) {
+
     for (std::{{#var:cont}}<int>::iterator it = c.begin(); it != c.end();)
         if (*it % 2 == 0) {
+
    {
 +
         if (*it % 2 == 0)
 
             it = c.erase(it);
 
             it = c.erase(it);
         } else {
+
         else
 
             ++it;
 
             ++it;
        }
 
 
     }
 
     }
 
     print_container(c);
 
     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 132: 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 clear |{{{1|}}}}}
+
{{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.

1) Removes the element at pos.
2) Removes the elements in the range [firstlast).

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.

1) If pos refers to the last element, then the end() iterator is returned.
2) If last == end() prior to removal, then the updated end() iterator is returned.
If [firstlast) 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) [edit]
clears the contents
(public member function of std::inplace_vector<T,N>) [edit]