std::forwar...erase_after(3)
π NAME π
std::forward_list::erase_after β std::forward_list::erase_after. Yep, thatβs the whole name twice, no cap.
Hiii bestie! π This is the std::forward_list::erase_after page from the C++ Standard Libary manual. Itβs giving βtiny but iconic member functionβ energy, and weβre gonna get you through it.
πΈ Synopsis πΈ
Okay so hereβs the synopsis, bestie β two overloads, both since C++11:
iterator erase_after( const_iterator pos );
(1) (since C++11)
iterator erase_after( const_iterator first, const_iterator last );
(2) (since C++11)
What it does: it removes specified elements from the container. Periodt.
- Removes the element following
pos. - Removes the elements following
firstuntillast.
π Parameters π
The params are lowkey specific, so pay attention bestie:
posβ iterator to the element preceding the element to removefirst, lastβ range of elements to remove
π Return value π
Hereβs what you get back:
- Overload (1) β Iterator to the element following the erased one, or
end()if no such element exists. - Overload (2) β
last.
π¦ Complexity π¦
- Overload (1) β Constant.
- Overload (2) β Linear in distance between
firstandlast.
π Example π
The example literally says // Run this code, so run it, bestie:
#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
π See also π
clearβ clears the contents (public member function)
Byeeeee π source/version is http://cppreference.com, date is 2024.06.10, and the footer label is std::forwar...erase_after(3). Stay iconic, bestie πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms