std::insert...r::operator=(3)
π 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 initer = container->insert(iter, value); ++iter;.(2)β results initer = 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, noconstexprfor you on these overloads, and if you're pre-C++11 the signature was theconst_referenceone. 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
- 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