std::forwar...insert_after(3)

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

✨ std::forward_list::insert_after ✨

okay bestie so listen β€” this is a page from the C++ Standard Library manual (section 3, mwah), and it's all about std::forward_list::insert_after. Like the name literally says: you insert stuff after a position. No insert_before girlies here, forward_list only goes one way and we respect it πŸ’…


🌸 SYNOPSIS 🌸

iterator insert_after( const_iterator pos, const T& value );

(1) (since C++11)

iterator insert_after( const_iterator pos, T&& value );

(2) (since C++11)

iterator insert_after( const_iterator pos, size_type count, const T& value );

(3) (since C++11)

template< class InputIt >
iterator insert_after( const_iterator pos, InputIt first, InputIt last );

(4) (since C++11)

iterator insert_after( const_iterator pos, std::initializer_list<T> ilist );

(5) (since C++11)


πŸ¦‹ DESCRIPTION πŸ¦‹

The whole vibe: inserts elements after the specified position in the container. That's it, that's the job, and she ate πŸ”₯

  • 1,2) β€” inserts value after the element pointed to by pos.
  • 3) β€” inserts count copies of the value after the element pointed to by pos.
  • 4) β€” inserts elements from the range [first, last) after the element pointed to by pos. ⚠️ The behavior is undefined if first and last are iterators into *this. Like fr fr do NOT do that, that's undefined behavior territory and we don't go there πŸ™ˆ
  • 5) β€” inserts elements from initializer list ilist.

No iterators or references are invalidated. Not a single one. she's careful like that πŸŽ€


πŸ’– PARAMETERS πŸ’–

  • pos β€” iterator after which the content will be inserted
  • value β€” element value to insert
  • count β€” number of copies to insert
  • first, last β€” the range of elements to insert
  • ilist β€” initializer list to insert the values from

⭐ TYPE REQUIREMENTS ⭐

  • InputIt must meet the requirements of LegacyInputIterator. (iykyk πŸ‘‘)

🌈 RETURN VALUE 🌈

  • 1,2) β€” iterator to the inserted element.
  • 3) β€” iterator to the last element inserted, or pos if count == 0.
  • 4) β€” iterator to the last element inserted, or pos if first == last.
  • 5) β€” iterator to the last element inserted, or pos if ilist is empty.

like she always gives you something back, even when nothing got inserted. main character energy πŸ’…


🧚 EXCEPTIONS 🧚

If an exception is thrown for any reason, these functions have no effect β€” that's the strong exception safety guarantee. lowkey iconic, she cleans up after herself ✨


πŸ“ COMPLEXITY πŸ“

  • 1,2) β€” Constant.
  • 3) β€” Linear in count.
  • 4) β€” Linear in std::distance(first, last).
  • 5) β€” Linear in ilist.size().

πŸ’œ EXAMPLE πŸ’œ

// Run this code

#include <forward_list>
#include <iostream>
#include <string>
#include <vector>

void print(const std::forward_list<int>& list)
{
    std::cout << "list: {";
    for (char comma[3] = {'\0', ' ', '\0'}; int i : list)
    {
        std::cout << comma << i;
        comma[0] = ',';
    }
    std::cout << "}\n";
}

int main()
{
    std::forward_list<int> ints{1, 2, 3, 4, 5};
    print(ints);

    // insert_after (2)
    auto beginIt = ints.begin();
    ints.insert_after(beginIt, -6);
    print(ints);

    // insert_after (3)
    auto anotherIt = beginIt;
    ++anotherIt;
    anotherIt = ints.insert_after(anotherIt, 2, -7);
    print(ints);

    // insert_after (4)
    const std::vector<int> v = {-8, -9, -10};
    anotherIt = ints.insert_after(anotherIt, v.cbegin(), v.cend());
    print(ints);

    // insert_after (5)
    ints.insert_after(anotherIt, {-11, -12, -13, -14});
    print(ints);
}

🫢 Output 🫢

list: {1, 2, 3, 4, 5}
list: {1, -6, 2, 3, 4, 5}
list: {1, -6, -7, -7, 2, 3, 4, 5}
list: {1, -6, -7, -7, -8, -9, -10, 2, 3, 4, 5}
list: {1, -6, -7, -7, -8, -9, -10, -11, -12, -13, -14, 2, 3, 4, 5}

look at all those negative numbers sliding in. obsessed 😭πŸ”₯


πŸŽ€ SEE ALSO πŸŽ€

  • emplace_after β€” constructs elements in-place after an element (public member function)
  • push_front β€” inserts an element to the beginning (public member function)

okay that's the whole page bestie, go forth and insert after πŸ’–

source/version: http://cppreference.com, dated 2024.06.10 β€” and this beauty is std::forwar...insert_after(3) (yes the title got cut off in the manual header, we don't know her full name either, it's giving mysterious 🌸). mwah, bye! πŸ’…βœ¨
πŸ“¦ where this page comes from