Namespaces
Variants
Views
Actions

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

From cppreference.com
m (r2.7.3) (Robot: Adding de, es, fr, it, ja, pt, ru, zh)
m (fmt)
 
(8 intermediate revisions by 5 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/navbar}}
 
{{cpp/container/forward_list/navbar}}
{{ddcl list begin}}
+
{{dcl begin}}
{{ddcl list item | num=1 | notes={{mark since c++11}} |
+
{{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 since c++11}} |
+
{{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.
1) iterator to the element following the erased one, or {{rlpf|end}} if no such element exists.
+
@2@ {{c|last}}
 
+
2) {{tt|last}}
+
  
 
===Complexity===
 
===Complexity===
 
+
@1@ Constant.
1) Constant.
+
@2@ Linear in distance between {{c|first}} and {{c|last}}.
 
+
2) linear in distance between {{tt|first}} and {{tt|last}}.
+
  
 
===Example===
 
===Example===
 
{{example
 
{{example
| code=
+
|code=
 
#include <forward_list>
 
#include <forward_list>
 +
#include <iostream>
 
#include <iterator>
 
#include <iterator>
#include <iostream>
+
 
 
int main()
 
int main()
 
{
 
{
     std::forward_list<int> l = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
+
     std::forward_list<int> l = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  
    //   l.erase( l.begin() ); // ERROR: No function erase
+
// l.erase(l.begin()); // Error: no function erase()
 
      
 
      
     l.erase_after( l.before_begin() ); // Removes first element
+
     l.erase_after(l.before_begin()); // Removes first element
 
      
 
      
     for( auto n : l ) std::cout << n << " ";
+
     for (auto n : l)
 +
        std::cout << n << ' ';
 
     std::cout << '\n';
 
     std::cout << '\n';
  
     auto fi= std::next( l.begin() );
+
     auto fi = std::next(l.begin());
     auto la= std::next( fi, 3 );
+
     auto la = std::next(fi, 3);
 
      
 
      
     l.erase_after( fi, la );
+
     l.erase_after(fi, la);
 
      
 
      
     for( auto n : l ) std::cout << n << " ";
+
     for (auto n : l)
 +
        std::cout << n << ' ';
 
     std::cout << '\n';
 
     std::cout << '\n';
 
}
 
}
| output=
+
|output=
 
2 3 4 5 6 7 8 9
 
2 3 4 5 6 7 8 9
 
2 3 6 7 8 9
 
2 3 6 7 8 9
Line 65: Line 63:
  
 
===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}}
+
 
+
[[de:cpp/container/forward list/erase after]]
+
[[es:cpp/container/forward list/erase after]]
+
[[fr:cpp/container/forward list/erase after]]
+
[[it:cpp/container/forward list/erase after]]
+
[[ja:cpp/container/forward list/erase after]]
+
[[pt:cpp/container/forward list/erase after]]
+
[[ru:cpp/container/forward list/erase after]]
+
[[zh:cpp/container/forward list/erase after]]
+

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]