std::front_inserter(3)

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

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 a push_front operation. That's the vibe check, bestie: if it can't push_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 a std::back_insert_iterator of type inferred from the argument. (function template)
  • inserter β€” creates a std::insert_iterator of 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