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

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

如同以如下方式,在 stack 中插入 rg 的各元素:


范围 rg 中的每个迭代器均恰好被解引用一次。

目录

[编辑] 参数

rg - 容器兼容范围,即其元素可以转换为 Tinput_range

[编辑] 复杂度

等于 c.append_rangeranges::copy(rg, std::back_inserter(c)) 的复杂度(取决于内部使用哪个函数)。

[编辑] 注解

功能特性测试 标准 功能特性
__cpp_lib_containers_ranges 202202L (C++23) 按范围构造和插入

[编辑] 示例

#include <initializer_list>
#include <stack>
#include <version>
#ifdef __cpp_lib_format_ranges
    #include <print>
    using std::println;
#else
    #define FMT_HEADER_ONLY
    #include <fmt/ranges.h>
    using fmt::println;
#endif
 
int main()
{
    std::stack<int> adaptor;
    const auto rg = {1, 3, 2, 4};
 
#ifdef __cpp_lib_containers_ranges
    adaptor.push_range(rg);
#else
    for (int e : rg)
        adaptor.push(e);
#endif
 
    println("{}", adaptor);
}

输出:

[1, 3, 2, 4]

[编辑] 参阅

向栈顶插入元素
(公开成员函数) [编辑]