std::priority_queue<T,Container,Compare>:: priority_queue
|
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,
|
(7) | (自 C++11 起) |
| (8) | ||
|
template
<
class
InputIt
>
priority_queue
(
InputIt first, InputIt last,
|
(C++11 前) | |
|
template
<
class
InputIt
>
priority_queue
(
InputIt first, InputIt last,
|
(自 C++11 起) | |
|
template
<
class
InputIt
>
priority_queue
(
InputIt first, InputIt last,
|
(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,
|
(12) | (自 C++11 起) |
|
template
<
class
Alloc
>
priority_queue
(
const
Compare
&
compare, Container
&&
cont,
|
(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,
|
(17) | (自 C++11 起) |
|
template
<
class
InputIt,
class
Alloc
>
priority_queue ( InputIt first, InputIt last, const</span |
从多种数据源构造容器适配器的新底层容器。
请注意,实现检查类型是否满足 LegacyInputIterator 的方式是未指定的,但要求必须拒绝整型类型。
目录 |
参数
| alloc | - | 用于底层容器所有内存分配的分配器 |
| other | - | 用作初始化底层容器来源的另一容器适配器 |
| cont | - | 用作初始化底层容器来源的容器 |
| compare | - | 用于初始化底层比较函数对象的比较函数对象 |
| first, last | - | 定义初始化元素 范围 的迭代器对 |
| rg | - |
容器兼容范围
,即元素可转换为
T
的
input_range
|
| 类型要求 | ||
-
Alloc
必须满足
Allocator
要求
|
||
-
Compare
必须满足
Compare
要求
|
||
-
Container
必须满足
Container
要求。仅当
Container
满足
AllocatorAwareContainer
要求时,分配器扩展构造函数才有定义
|
||
-
InputIt
必须满足
LegacyInputIterator
要求
|
||
复杂度
value_type
构造函数的调用,其中
N
是
cont.
size
(
)
。
value_type
构造函数的调用,其中
N
为
cont.
size
(
)
,
M
为
std::
distance
(
first, last
)
。
Alloc
与
other
的分配器比较相等时为常数复杂度。否则与
other
的大小成线性关系。
value_type
构造函数的调用(当
Alloc
与
other
的分配器不相等时存在),其中
N
为
cont.
size
(
)
,
M
为
std::
distance
(
first, last
)
。
| 此章节内容不完整 |
备注
| 功能测试 宏 | 值 | 标准 | 功能特性 |
|---|---|---|---|
__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
|
直接通过迭代器对构造容器 |
参见
|
为容器适配器赋值
(公开成员函数) |