std::stack::push_range(3)

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

πŸ’– NAME πŸ’–

std::stack::push_range β€” std::stack::push_range. That's the whole name, bestie β€” no cap, it's giving "the name is the name" energy πŸ’….

okay so this page is std::stack::push_range(3), straight outta the C++ Standard Libary (their spelling, not mine 🫢).

✨ Synopsis ✨

template< container-compatible-range<value_type> R >  (since C++23)
void push_range( R&& rg );

Since C++23, this is the little range-insertion slay. It inserts a copy of each element of rg in stack, as if by:

  • c.append_range(std::forward<R>(rg)) if that is a valid expression (i.e. the underlying container c has an appropriate append_range member function), or
  • ranges::copy(rg, std::back_inserter(c)) otherwise.

Important vibe check: each iterator in the range rg is dereferenced exactly once. Not twice. Once. Periodt ✨.

πŸŽ€ Parameters πŸŽ€

  • rg β€” a container compatible range, that is, an input_range whose elements are convertible to T.

✨ Return value ✨

(none) β€” literally nothing, bestie 🫢.

πŸŒ€ Complexity πŸŒ€

Identical to the complexity of c.append_range or ranges::copy(rg, std::back_inserter(c)) (depending on what function is used internally).

πŸ“ Notes πŸ“

Feature-test macro tea β˜•:

  • __cpp_lib_containers_ranges β€” value 202202L, Std (C++23), feature: "Ranges-aware construction and insertion".

πŸ’» Example πŸ’»

       // Run this code

        #include <algorithm>
        #include <iostream>
        #include <ranges>
        #include <stack>

        template<typename Adaptor>
        requires (std::ranges::input_range<typename Adaptor::container_type>)
        void println(auto, const Adaptor& adaptor)
        {
            struct Container : Adaptor // gain access to protected
       Adaptor::Container c;
            {
                auto const& container() const { return this->c; }
            };

            for (auto const& elem : static_cast<const
       Container&>(adaptor).container())
                std::cout << elem << ' ';
            std::cout << '\n';
        }

        int main()
        {
            std::stack<int> adaptor;
            const auto rg = {1, 3, 2, 4};

        #ifdef __cpp_lib_containers_ranges
            adaptor.push_range(rg);
        #else
            std::ranges::for_each(rg, [&adaptor](auto e){ adaptor.push(e); });
        #endif

            println("{}", adaptor);
        }

πŸ–¨οΈ Output πŸ–¨οΈ

1 3 2 4

πŸ‘€ See also πŸ‘€

  • push β€” inserts element at the top (public member function).

source/version line: http://cppreference.com Β· date: 2024.06.10 Β· page: std::stack::push_range(3) β€” mwah, go push some ranges bestie πŸ’–

πŸ“¦ where this page comes from