std::front_inserter(3)
std::front_inserter(3)
Okay so this page lives under the C++ Standard Libary manual name β yes, that's the spelling in the header, bestie, and we're keeping it π. It's the std::front_inserter page, and it's giving tiny-helper-that-slays energy. π
π NAME π
std::front_inserter β literally std::front_inserter. That's the name. No cap, it's giving "the function is the name" main character energy. β¨
πΈ Synopsis πΈ
Defined in header <iterator>
template< class Container >
constexpr std::front_insert_iterator<Container> front_inserter( Container& c );
It's constexpr since C++20, bestie β the manpage note is (constexpr since C++20), and yes, that's iconic. π
Okay so front_inserter is a convenience function template π«Ά. It constructs a std::front_insert_iterator for the container c, and the type is deduced from the type of the argument. It's giving helper-function main character energy, fr fr. β¨
π Parameters π
cβ a container that supports apush_frontoperation. That's the vibe check, bestie: if it can'tpush_front, it's not passing. π
π¦ Return value π¦
Returns a std::front_insert_iterator β and you can use it to add elements to the beginning of the container c. Slay. π
β¨ Possible implementation β¨
Here's the possible implementation, and yes it's literally this simple:
template<class Container>
std::front_insert_iterator<Container> front_inserter( Container& c )
{
return std::front_insert_iterator<Container>(c);
}
πΈ Example πΈ
Run this code, bestie:
// Run this code
#include <algorithm>
#include <deque>
#include <iostream>
#include <iterator>
#include <vector>
int main()
{
std::vector<int> v{1, 2, 3, 4, 5};
std::deque<int> d;
std::copy(v.begin(), v.end(), std::front_inserter(d));
for (int n : d)
std::cout << n << ' ';
std::cout << '\n';
}
π Output π
5 4 3 2 1 β the example ate and left no crumbs. π
π See also π
front_insert_iteratorβ iterator adaptor for insertion at the front of a container. (class template)back_inserterβ creates astd::back_insert_iteratorof type inferred from the argument. (function template)inserterβ creates astd::insert_iteratorof type inferred from the argument. (function template)
mwah π β this page's source is http://cppreference.com and its version/date is 2024.06.10. Bye bestie! π«Ά
π¦ 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