命名空间
变体
操作

std::priority_queue<T,Container,Compare>::push_range

来自 cppreference.cn
< cpp‎ | 容器‎ | 优先队列
template< container-compatible-range<value_type> R >
void push_range( R&& rg );
(自 C++23 起)

rg 中每个元素的副本插入 priority_queue,如同通过

然后恢复堆属性,如同通过 ranges::make_heap(c, comp)。插入后,ranges::is_heap(c, comp)true

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

内容

[编辑] 参数

rg - 一个容器兼容范围,即一个input_range,其元素可转换为 T

[编辑] 复杂度

c.append_range 的复杂度加上 ranges::make_heap(c, comp) 的复杂度。

[编辑] 注解

特性测试 Std 特性
__cpp_lib_containers_ranges 202202L (C++23) 范围感知 构造和插入

[编辑] 示例

#include <initializer_list>
#include <queue>
#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::priority_queue<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);
}

输出

[4, 3, 2, 1]

[编辑] 参见

插入元素并排序底层容器
(公共成员函数) [编辑]