Namespaces
Variants
Views
Actions

std::stack<T,Container>::push_range

From cppreference.com
< cpp‎ | container‎ | stack
 
 
 
 
template< container-compatible-range<value_type> R >
void push_range( R&& rg );
(since C++23)

Inserts a copy of each element of rg in stack, as if by:


Each iterator in the range rg is dereferenced exactly once.

Contents

[edit] Parameters

rg - a container compatible range, that is, an input_range whose elements are convertible to T

[edit] Return value

(none)

[edit] Complexity

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

[edit] Notes

Feature-test macro Value Std Feature
__cpp_lib_containers_ranges 202202L (C++23) Ranges-aware construction and insertion

[edit] Example

#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

[edit] See also

inserts element at the top
(public member function) [edit]