std::back_inserter(3)
std::back_inserter(3) β C++ Standard Libary π
okay bestie, welcome to the <hotpink>std::back_inserter(3)</hotpink> page from the <yellow>C++ Standard Libary</yellow> manual β yes, that's how the header spells it, and we're keeping the typo because details are sacred. π
This one is short, sweet, and highkey iconic. β¨
π NAME π
std::back_inserter β literally std::back_inserter. no cap, that's the name, periodt. π«Ά
π¦ Synopsis π¦
The way that this lives in <iterator> is giving header-file main character energy:
Defined in header <iterator>
template< class Container >
std::back_insert_iterator<Container> back_inserter( Container& c );
and the synopsis also says (constexpr since C++20), so there's your version tea. ββ¨
back_inserter is a convenience function template that constructs a std::back_insert_iterator for the container c with the type deduced from the type of the argument. it's literally the convenience-function bestie of your push_back dreams. π
πΈ Parameters πΈ
cβ container that supports apush_backoperation. that's the whole vibe check. π«Ά
π Return value π
A std::back_insert_iterator which can be used to add elements to the end of the container c. you get the iterator, it adds at the back, slay. π
π§ Possible implementation π§
not me showing you the lowkey implementation, bestie π:
template<class Container>
std::back_insert_iterator<Container> back_inserter(Container& c)
{
return std::back_insert_iterator<Container>(c);
}
π₯ Example π₯
ok this example ate, ngl. run this code:
// Run this code
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main()
{
std::vector<int> v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::fill_n(std::back_inserter(v), 3, -1);
for (int n : v)
std::cout << n << ' ';
std::cout << '\n';
}
π Output π
it prints 1 2 3 4 5 6 7 8 9 10 -1 -1 -1 β periodt. ππ
π See also π
back_insert_iteratorβ iterator adaptor for insertion at the end of a container (class template)front_inserterβ creates astd::front_insert_iteratorof type inferred from the argument (function template)inserterβ creates astd::insert_iteratorof type inferred from the argument (function template)
π source:http://cppreference.comΒ· version + date:2024.06.10Β· page:std::back_inserter(3)β bye bestie, gopush_backresponsibly! π«Άβ¨
π¦ 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