std::inserter(3)

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

πŸ’– NAME πŸ’–

Name situation: std::inserter β€” std::inserter, bestie. This is manual page std::inserter(3) from the C++ Standard Libary (yes, that’s the header’s spelling β€” iconic typo behavior πŸ’€), and we’re about to make this convenience function make sense.


✨ Synopsis ✨

Okay so this one is defined in the header <iterator>, and both the until C++20 and since C++20 eras are invited to the group chat:

Defined in header <iterator>
template< class Container >

std::insert_iterator<Container>
(until C++20)

    inserter( Container& c, typename Container::iterator i );
template< class Container >

constexpr std::insert_iterator<Container>
(since C++20)

    inserter( Container& c, ranges::iterator_t<Container> i );

inserter is a convenience function template that constructs a std::insert_iterator for the container c and its iterator i, with the type deduced from the type of the argument. no cap, that’s the whole flex πŸ’…


πŸŽ€ Parameters πŸŽ€

  • c β€” container that supports an insert operation.
  • i β€” iterator in c indicating the insertion position.

🌸 Return value 🌸

A std::insert_iterator which can be used to insert elements into the container c at the position indicated by i. slay. 🫢


πŸ› οΈ Possible implementation πŸ› οΈ

Here’s the possible implementation β€” lowkey it just wraps the constructor:

template<class Container>
std::insert_iterator<Container> inserter(Container& c, typename Container::iterator i)
{
    return std::insert_iterator<Container>(c, i);
}

🧚 Example 🧚

// Run this code β€” that’s the cppreference button energy, but here’s the actual code:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <set>
#include <vector>

int main()
{
    std::multiset<int> s{1, 2, 3};

    // std::inserter is commonly used with multi-sets
    std::fill_n(std::inserter(s, s.end()), 5, 2);

    for (int n : s)
        std::cout << n << ' ';
    std::cout << '\n';

    std::vector<int> d{100, 200, 300};
    std::vector<int> v{1, 2, 3, 4, 5};

    // when inserting in a sequence container, insertion point advances
    // because each std::insert_iterator::operator= updates the target iterator
    std::copy(d.begin(), d.end(), std::inserter(v, std::next(v.begin())));

    for (int n : v)
        std::cout << n << ' ';
    std::cout << '\n';
}

🌈 Output 🌈

1 2 2 2 2 2 2 3
1 100 200 300 2 3 4 5

πŸ’€ Defect reports πŸ’€

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

  • DR: LWG 561 β€” Applied to: C++98. Behavior as published: the type of i was independent of Container. Correct behavior: it is the iterator type of Container.

πŸ‘‘ See also πŸ‘‘

  • insert_iterator β€” iterator adaptor for insertion into a container (class template)
  • back_inserter β€” creates a std::back_insert_iterator of type inferred from the argument (function template)
  • front_inserter β€” creates a std::front_insert_iterator of type inferred from the argument (function template)

That’s the whole page, bestie 🫢 β€” source/version: http://cppreference.com, dated 2024.06.10, folded into this cute little sign-off instead of a boring footer ✨

πŸ“¦ where this page comes from