Namespaces
Variants

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

From cppreference.net
priority_queue ( ) : priority_queue ( Compare ( ) , Container ( ) ) { }
(1) (自 C++11 起)
explicit priority_queue ( const Compare & compare )
: priority_queue ( compare, Container ( ) ) { }
(2) (自 C++11 起)
(3)
explicit priority_queue ( const Compare & compare = Compare ( ) ,
const Container & cont = Container ( ) ) ;
(C++11 前)
priority_queue ( const Compare & compare, const Container & cont ) ;
(自 C++11 起)
priority_queue ( const Compare & compare, Container && cont ) ;
(4) (自 C++11 起)
priority_queue ( const priority_queue & other ) ;
(5)
priority_queue ( priority_queue && other ) ;
(6) (自 C++11 起)
template < class InputIt >

priority_queue ( InputIt first, InputIt last,

const Compare & compare = Compare ( ) ) ;
(7) (自 C++11 起)
(8)
template < class InputIt >

priority_queue ( InputIt first, InputIt last,
const Compare & compare = Compare ( ) ,

const Container & cont = Container ( ) ) ;
(C++11 前)
template < class InputIt >

priority_queue ( InputIt first, InputIt last,

const Compare & compare, const Container & cont ) ;
(自 C++11 起)
template < class InputIt >

priority_queue ( InputIt first, InputIt last,

const Compare & compare, Container && cont ) ;
(9) (自 C++11 起)
template < class Alloc >
explicit priority_queue ( const Alloc & alloc ) ;
(10) (自 C++11 起)
template < class Alloc >
priority_queue ( const Compare & compare, const Alloc & alloc ) ;
(11) (自 C++11 起)
template < class Alloc >

priority_queue ( const Compare & compare, const Container & cont,

const Alloc & alloc ) ;
(12) (自 C++11 起)
template < class Alloc >

priority_queue ( const Compare & compare, Container && cont,

const Alloc & alloc ) ;
(13) (自 C++11 起)
template < class Alloc >
priority_queue ( const priority_queue & other, const Alloc & alloc ) ;
(14) (自 C++11 起)
template < class Alloc >
priority_queue ( priority_queue && other, const Alloc & alloc ) ;
(15) (自 C++11 起)
template < class InputIt, class Alloc >
priority_queue ( InputIt first, InputIt last, const Alloc & alloc ) ;
(16) (自 C++11 起)
template < class InputIt, class Alloc >

priority_queue ( InputIt first, InputIt last, const Compare & compare,

const Alloc & alloc ) ;
(17) (自 C++11 起)
template < class InputIt, class Alloc >

priority_queue ( InputIt first, InputIt last, const</span

从多种数据源构造容器适配器的新底层容器。

1) 默认构造函数。值初始化比较器和底层容器。
2) 使用 compare 的内容复制构造比较函数对象 comp 。对底层容器 c 进行值初始化。
3) 使用 cont 的内容复制构造底层容器 c 。使用 compare 的内容复制构造比较函数对象 comp 。调用 std:: make_heap ( c. begin ( ) , c. end ( ) , comp ) 此构造函数同时也是默认构造函数。 (C++11 前)
4) 使用 std :: move ( cont ) 移动构造底层容器 c 。使用 compare 拷贝构造比较函数对象 comp 。调用 std:: make_heap ( c. begin ( ) , c. end ( ) , comp )
5) 复制构造函数 。底层容器通过 other. c 进行复制构造。比较函数对象通过 other. comp 进行复制构造。 (隐式声明)
6) 移动构造函数 。底层容器通过 std :: move ( other. c ) 构造。比较函数对象通过 std :: move ( other. comp ) 构造。 (隐式声明)
7-9) 迭代器对构造函数。仅当 InputIt 满足 LegacyInputIterator 时,这些重载才会参与重载决议。
7) 构造 c 的方式如同通过 c ( first, last ) ,并从 compare 构造 comp 。随后调用 std:: make_heap ( c. begin ( ) , c. end ( ) , comp ) ;
8) cont 复制构造 c ,并从 compare 复制构造 comp 。随后调用 c. insert ( c. end ( ) , first, last ) ; ,再调用 std:: make_heap ( c. begin ( ) , c. end ( ) , comp ) ;
9) std :: move ( cont ) 移动构造 c 并从 compare 复制构造 comp 。随后调用 c. insert ( c. end ( ) , first, last ) ; ,再调用 std:: make_heap ( c. begin ( ) , c. end ( ) , comp ) ;
10-15) 分配器扩展构造函数。这些重载仅当 std:: uses_allocator < container_type, Alloc > :: value true 时参与重载决议,即当底层容器是分配器感知容器时(所有标准库容器均满足此条件)。
10) 使用 alloc 作为分配器构造底层容器。实际调用 c ( alloc ) comp 进行值初始化。
11) 使用 alloc 作为分配器构造底层容器。实际调用 c ( alloc ) 。从 compare 复制构造 comp
12) 使用 cont 的内容和 alloc 作为分配器构造底层容器,如同通过 c ( cont, alloc ) 进行构造。从 compare 复制构造 comp 。随后调用 std:: make_heap ( c. begin ( ) , c. end ( ) , comp )
13) 使用移动语义构造底层容器,以 alloc 作为分配器,内容为 cont ,形如 c ( std :: move ( cont ) , alloc ) 。从 compare 复制构造 comp 。随后调用 std:: make_heap ( c. begin ( ) , c. end ( ) , comp )
14) 使用 other. c 的内容和 alloc 作为分配器构造底层容器。实际调用 c ( other. c , alloc ) 。从 other. comp 复制构造 comp
15) 使用移动语义构造底层容器,以 other 的内容初始化,同时使用 alloc 作为分配器。实际调用 c ( std :: move ( other. c ) , alloc ) 。从 other. comp 移动构造 comp
16-19) 分配器扩展的迭代器对构造函数。与 (7-9) 相同,但使用 alloc 构造底层容器。仅当 std:: uses_allocator < container_type, Alloc > :: value true InputIt 满足 LegacyInputIterator 时,这些重载参与重载决议。
20) 使用 compare 初始化 comp ,并使用 ranges:: to < Container > ( std:: forward < R > ( rg ) ) 初始化 c 。随后调用 std:: make_heap ( c. begin ( ) , c. end ( ) , comp )
21) 使用 compare 初始化 comp ,并使用 ranges:: to < Container > ( std:: forward < R > ( rg ) , alloc ) 初始化 c 。随后调用 std:: make_heap ( c. begin ( ) , c. end ( ) , comp )
22) 使用 ranges:: to < Container > ( std:: forward < R > ( rg ) , alloc ) 初始化 c 。随后调用 std:: make_heap ( c. begin ( ) , c. end ( ) , comp )

请注意,实现检查类型是否满足 LegacyInputIterator 的方式是未指定的,但要求必须拒绝整型类型。

目录

参数

alloc - 用于底层容器所有内存分配的分配器
other - 用作初始化底层容器来源的另一容器适配器
cont - 用作初始化底层容器来源的容器
compare - 用于初始化底层比较函数对象的比较函数对象
first, last - 定义初始化元素 范围 的迭代器对
rg - 容器兼容范围 ,即元素可转换为 T input_range
类型要求
-
Alloc 必须满足 Allocator 要求
-
Compare 必须满足 Compare 要求
-
Container 必须满足 Container 要求。仅当 Container 满足 AllocatorAwareContainer 要求时,分配器扩展构造函数才有定义
-
InputIt 必须满足 LegacyInputIterator 要求

复杂度

1,2) 常量。
3,5,12) O(N) 次比较和 O(N) 次对 value_type 构造函数的调用,其中 N cont. size ( )
4) O(N) 次比较,其中 N cont. size ( )
6) 常量。
7,16,17) O(M) 次比较,其中 M std:: distance ( first, last )
8,18) O(N + M) 次比较和 O(N) 次对 value_type 构造函数的调用,其中 N cont. size ( ) M std:: distance ( first, last )
9) O(N + M) 次比较,其中 N cont. size ( ) M std:: distance ( first, last )
10,11) 常量。
13) O(N) 次比较,其中 N cont. size ( )
14) other 的大小呈线性关系。
15) Alloc other 的分配器比较相等时为常数复杂度。否则与 other 的大小成线性关系。
19) O(N + M) 次比较以及可能 O(N) 次对 value_type 构造函数的调用(当 Alloc other 的分配器不相等时存在),其中 N cont. size ( ) M std:: distance ( first, last )
20) O(N) 次比较和 O(N) 次对 value_type 构造函数的调用,其中 N ranges:: distance ( rg )
21,22)

备注

功能测试 标准 功能特性
__cpp_lib_containers_ranges 202202L (C++23) 支持范围 的构造与插入;重载版本 ( 20-22 )

示例

#include <complex>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
int main()
{
    std::priority_queue<int> pq1;
    pq1.push(5);
    std::cout << "pq1.size() = " << pq1.size() << '\n';
    std::priority_queue<int> pq2 {pq1};
    std::cout << "pq2.size() = " << pq2.size() << '\n';
    std::vector<int> vec {3, 1, 4, 1, 5};
    std::priority_queue<int> pq3 {std::less<int>(), vec};
    std::cout << "pq3.size() = " << pq3.size() << '\n';
    for (std::cout << "pq3 : "; !pq3.empty(); pq3.pop())
        std::cout << pq3.top() << ' ';
    std::cout << '\n';
    // 自定义比较器演示:
    using my_value_t = std::complex<double>;
    using my_container_t = std::vector<my_value_t>;
    auto my_comp = [](const my_value_t& z1, const my_value_t& z2)
    {
        return z2.real() < z1.real();
    };
    std::priority_queue<my_value_t,
                        my_container_t,
                        decltype(my_comp)> pq4{my_comp};
    using namespace std::complex_literals;
    pq4.push(5.0 + 1i);
    pq4.push(3.0 + 2i);
    pq4.push(7.0 + 3i);
    for (; !pq4.empty(); pq4.pop())
    {
        const auto& z = pq4.top();
        std::cout << "pq4.top() = " << z << '\n';
    }
    // TODO: C++23 范围感知构造函数
}

输出:

pq1.size() = 1
pq2.size() = 1
pq3.size() = 5
pq3 : 5 4 3 1 1
pq4.top() = (3,2)
pq4.top() = (5,1)
pq4.top() = (7,3)

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 适用范围 发布时的行为 正确行为
P0935R0 C++11 默认构造函数和构造函数 (4) 为显式 改为隐式
LWG 3506 C++11 缺少分配器扩展的迭代器对构造函数 已添加
LWG 3522 C++11 缺少迭代器对构造函数的约束条件 已添加
LWG 3529 C++11 通过迭代器对构造时调用 insert 直接通过迭代器对构造容器

参见

为容器适配器赋值
(公开成员函数)