Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/container/forward list/erase after"

From cppreference.com
m (Text replace - "{{returns}}" to "===Return value===")
m (fmt)
 
(18 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{cpp/container/forward_list/title | erase_after}}
+
{{cpp/container/forward_list/title|erase_after}}
{{cpp/container/forward_list/sidebar}}
+
{{cpp/container/forward_list/navbar}}
{{ddcl list begin}}
+
{{dcl begin}}
{{ddcl list item | num=1 | notes={{mark c++0x feature}} |
+
{{dcl|num=1|since=c++11|
iterator erase_after( const_iterator position );
+
iterator erase_after( const_iterator pos );
 
}}
 
}}
{{ddcl list item | num=2 | notes={{mark c++0x feature}} |
+
{{dcl|num=2|since=c++11|
 
iterator erase_after( const_iterator first, const_iterator last );
 
iterator erase_after( const_iterator first, const_iterator last );
 
}}
 
}}
{{ddcl list end}}
+
{{dcl end}}
  
Removes specified elements from the container.  
+
Removes specified elements from the container.
  
1) Removes the element following {{tt|pos}}.
+
@1@ Removes the element following {{c|pos}}.
 +
@2@ Removes the elements following {{c|first}} until {{c|last}}.
  
2) Removes the elements in the range {{tt|(first; last)}}.
 
 
<!-- ======== -->
 
 
===Parameters===
 
===Parameters===
{{param list begin}}
+
{{par begin}}
{{param list item | pos | iterator to the element preceding the element to remove}}
+
{{par|pos|iterator to the element preceding the element to remove}}
{{param list item | first, last | range of elements to remove}}
+
{{par|first, last|range of elements to remove}}
{{param list end}}  
+
{{par end}}
  
<!-- ======== -->
 
 
===Return value===
 
===Return value===
 +
@1@ Iterator to the element following the erased one, or {{lc|end()}} if no such element exists.
 +
@2@ {{c|last}}
  
1) iterator to the element following the erased one, or {{rlpf|end}} if no such element exists.
+
===Complexity===
 +
@1@ Constant.
 +
@2@ Linear in distance between {{c|first}} and {{c|last}}.
  
2) {{tt|last}}
+
===Example===
 +
{{example
 +
|code=
 +
#include <forward_list>
 +
#include <iostream>
 +
#include <iterator>
  
<!-- ======== -->
+
int main()
{{example}}
+
{
{{example cpp
+
    std::forward_list<int> l = {1, 2, 3, 4, 5, 6, 7, 8, 9};
| code=
+
| output=
+
}}
+
  
<!-- ======== -->
+
//  l.erase(l.begin()); // Error: no function erase()
{{complex}}
+
   
 +
    l.erase_after(l.before_begin()); // Removes first element
 +
   
 +
    for (auto n : l)
 +
        std::cout << n << ' ';
 +
    std::cout << '\n';
  
1) {{complex constant}}.
+
    auto fi = std::next(l.begin());
 
+
    auto la = std::next(fi, 3);
2) linear in distance between {{tt|first}} and {{tt|last}}.
+
   
 +
    l.erase_after(fi, la);
 +
   
 +
    for (auto n : l)
 +
        std::cout << n << ' ';
 +
    std::cout << '\n';
 +
}
 +
|output=
 +
2 3 4 5 6 7 8 9
 +
2 3 6 7 8 9
 +
}}
  
<!-- ======== -->
 
 
===See also===
 
===See also===
 +
{{dsc begin}}
 +
{{dsc inc|cpp/container/dsc clear|forward_list}}
 +
{{dsc end}}
  
{{dcl list begin}}
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
{{dcl list template | cpp/container/dcl list clear |forward_list}}
+
{{dcl list end}}
+

Latest revision as of 09:14, 27 September 2023

 
 
 
 
iterator erase_after( const_iterator pos );
(1) (since C++11)
iterator erase_after( const_iterator first, const_iterator last );
(2) (since C++11)

Removes specified elements from the container.

1) Removes the element following pos.
2) Removes the elements following first until last.

Contents

[edit] Parameters

pos - iterator to the element preceding the element to remove
first, last - range of elements to remove

[edit] Return value

1) Iterator to the element following the erased one, or end() if no such element exists.
2) last

[edit] Complexity

1) Constant.
2) Linear in distance between first and last.

[edit] Example

#include <forward_list>
#include <iostream>
#include <iterator>
 
int main()
{
    std::forward_list<int> l = {1, 2, 3, 4, 5, 6, 7, 8, 9};
 
//  l.erase(l.begin()); // Error: no function erase()
 
    l.erase_after(l.before_begin()); // Removes first element
 
    for (auto n : l)
        std::cout << n << ' ';
    std::cout << '\n';
 
    auto fi = std::next(l.begin());
    auto la = std::next(fi, 3);
 
    l.erase_after(fi, la);
 
    for (auto n : l)
        std::cout << n << ' ';
    std::cout << '\n';
}

Output:

2 3 4 5 6 7 8 9
2 3 6 7 8 9

[edit] See also

clears the contents
(public member function) [edit]