Difference between revisions of "cpp/container/forward list/erase after"
From cppreference.com
< cpp | container | forward list
m (r2.7.3) (Robot: Adding de, es, fr, it, ja, pt, ru, zh) |
Andreas Krug (Talk | contribs) 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}} | ||
− | {{ | + | {{dcl begin}} |
− | {{ | + | {{dcl|num=1|since=c++11| |
− | iterator erase_after( const_iterator | + | iterator erase_after( const_iterator pos ); |
}} | }} | ||
− | {{ | + | {{dcl|num=2|since=c++11| |
iterator erase_after( const_iterator first, const_iterator last ); | iterator erase_after( const_iterator first, const_iterator last ); | ||
}} | }} | ||
− | {{ | + | {{dcl end}} |
− | Removes specified elements from the container. | + | Removes specified elements from the container. |
− | 1 | + | @1@ Removes the element following {{c|pos}}. |
− | + | @2@ Removes the elements following {{c|first}} until {{c|last}}. | |
− | 2 | + | |
===Parameters=== | ===Parameters=== | ||
− | {{ | + | {{par begin}} |
− | {{ | + | {{par|pos|iterator to the element preceding the element to remove}} |
− | {{ | + | {{par|first, last|range of elements to remove}} |
− | {{ | + | {{par end}} |
===Return value=== | ===Return value=== | ||
− | + | @1@ Iterator to the element following the erased one, or {{lc|end()}} if no such element exists. | |
− | 1 | + | @2@ {{c|last}} |
− | + | ||
− | 2 | + | |
===Complexity=== | ===Complexity=== | ||
− | + | @1@ Constant. | |
− | 1 | + | @2@ Linear in distance between {{c|first}} and {{c|last}}. |
− | + | ||
− | 2 | + | |
===Example=== | ===Example=== | ||
{{example | {{example | ||
− | + | |code= | |
#include <forward_list> | #include <forward_list> | ||
+ | #include <iostream> | ||
#include <iterator> | #include <iterator> | ||
− | + | ||
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_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= | |
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}} | ||
− | {{ | + | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + |
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
Run this code
#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) |