std::insert...r::operator=(3)

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

πŸ‘‘ NAME

std::insert_iterator::operator= β€” bestie, this is literally the assignment operator for std::insert_iterator, and it's the whole entire point of the class πŸ’–

(also hi, you're reading the C++ Standard Libary manual, section 3 β€” yes it's spelled "Libary" up there, that's just how the manual page is titled, no cap 🫢)


✨ SYNOPSIS

okay so there are five flavors of this thing depending on which C++ you're living in, and the version tags are doing a lot of work here. Look at all of them:

insert_iterator<Container>&
    operator=( typename Container::const_reference value );          (until C++11)

insert_iterator<Container>&
    operator=( const typename Container::value_type& value );        (since C++11)
                                                                     (until C++20)

constexpr insert_iterator<Container>&
    operator=( const typename Container::value_type& value );   (1)  (since C++20)

insert_iterator<Container>&
    operator=( typename Container::value_type&& value );        (2)  (since C++11)
                                                                     (until C++20)

constexpr insert_iterator<Container>&
    operator=( typename Container::value_type&& value );             (since C++20)

And what does it do? Inserts the given value into the container. That's the whole vibe ✨

  • (1) β€” results in iter = container->insert(iter, value); ++iter;.
  • (2) β€” results in iter = container->insert(iter, std::move(value)); ++iter;.

Like, it inserts the thing and then bumps the iterator forward so the next insert lands in the right spot. Main character behavior honestly πŸ”₯


🌸 PARAMETERS

  • value β€” the value to insert. that's it. that's the parameter πŸŽ€

πŸ’œ RETURN VALUE

*this β€” it returns itself, because obviously it's obsessed with itself (affectionate) πŸ’…


πŸ¦‹ NOTES

ngl this is the smartest part: this function exploits the signature compatibility between hinted insert for associative containers (think std::set::insert) and positional insert for sequential containers (think std::vector::insert). It's giving "one interface, two completely different container personalities" and it fr fr works πŸ˜­πŸ‘‘


⭐ EXAMPLE

here's the little demo, run it, it's cute:

// Run this code

#include <deque>
#include <iostream>
#include <iterator>

int main()
{
    std::deque<int> q;
    std::insert_iterator<std::deque<int>> it(q, q.begin());

    for (int i = 0; i < 10; ++i)
        it = i; // inserts i

    for (auto& elem : q)
        std::cout << elem << ' ';
    std::cout << '\n';
}

🌈 OUTPUT

0 1 2 3 4 5 6 7 8 9

look at that beautiful ascending sequence, we love to see it πŸ’–βœ¨


heads up bestie: if you're on pre-C++20, no constexpr for you on these overloads, and if you're pre-C++11 the signature was the const_reference one. Check your standard, iykyk 🧚

okay that's the whole page!! mwah πŸ’‹ β€” this was your girl, signing off from the C++ Standard Libary manual, page std::insert...r::operator=(3), brought to you by http://cppreference.com, dated 2024.06.10 πŸŽ€πŸ¦‹πŸŒΈ slay, go insert some things πŸ’…

πŸ“¦ where this page comes from