std::forwar...erase_after(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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.

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

πŸŽ€ Parameters πŸŽ€

The params are lowkey specific, so pay attention bestie:

  • pos β€” iterator to the element preceding the element to remove
  • first, 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 first and last.

🌈 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