std::inserter(3)
π 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 aninsertoperation.iβ iterator incindicating 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 ofiwas independent ofContainer. Correct behavior: it is the iterator type ofContainer.
π See also π
insert_iteratorβ iterator adaptor for insertion into a container (class template)back_inserterβ creates astd::back_insert_iteratorof type inferred from the argument (function template)front_inserterβ creates astd::front_insert_iteratorof 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
- 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