std::priority_queue<T,Container,Compare>::priority_queue_C++中文网
priority_queue( const Compare& compare, const Container& cont,
const Alloc& alloc );
template< class Alloc >
priority_queue( const Compare& compare, Container&& cont,
template< class Alloc >
priority_queue( const priority_queue& other, const Alloc& alloc );
template< class Alloc >
priority_queue( priority_queue&& other, const Alloc& alloc );
template< class InputIt >
priority_queue( InputIt first, InputIt last,
template< class InputIt >
priority_queue( InputIt first, InputIt last,
const Compare& compare = Compare(),
从多种数据源构造容器适配器的底层容器。
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-12) 仅若 std::uses_allocator<container_type, Alloc>::value == true ,即若底层容器是具分配器容器(对所有标准库容器为真),才定义下列构造函数。
7) 以 alloc 为分配器构造底层容器。等效地调用 c(alloc) 。值初始化 comp 。
8) 以 alloc 为分配器构造底层容器。等效地调用 c(alloc) 。从 compare 复制构造 comp 。
9) 用 cont 的内容,以 alloc 为分配器构造底层容器,如同用 c(cont, alloc) 。从 compare 复制构造 comp 。然后调用 std::make_heap(c.begin(), c.end(), comp) 。
10) 用 cont 的内容,以 alloc 为分配器并以移动语义构造底层容器,如同用 c(std::move(cont), alloc) 。从 compare 复制构造 comp 。然后调用 std::make_heap(c.begin(), c.end(), comp) 。
11) 用 other.c 的内容,以 alloc 为分配器构造底层容器。等效地调用 c(other.c, alloc) 。从 other.comp 复制构造 comp 。
12) 用 other 的内容,同时以 alloc 为分配器构造适配器。等效地调用 c(std::move(other.c), alloc) 。从 other.comp 移动构造 comp 。
13) 从 cont 复制构造 c 并从 compare 复制构造 comp 。然后调用 c.insert(c.end(), first, last); ,再调用 std::make_heap(c.begin(), c.end(), comp); 。
14) 从 std::move(cont) 移动构造 c 并从 std::move(compare) 移动构造 comp 。然后调用 c.insert(c.end(), first, last); ,再调用 std::make_heap(c.begin(), c.end(), comp); 。
参数
| alloc | - | 用于底层容器所有内存分配的分配器 |
| other | - | 用作源初始化底层容器的另一容器适配器 |
| cont | - | 用作源初始化底层容器的容器 |
| compare | - | 用于初始化底层比较函数对象的比较函数对象 |
| first, last | - | 要初始化的元素范围 |
| 类型要求 | ||
-Alloc 必须满足分配器 (Allocator) 的要求。
| ||
-Container 必须满足容器 (Container) 的要求。仅若 Container 满足具分配器容器 (AllocatorAwareContainer) 的要求才定义构造函数 (5-10)
| ||
-InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。
| ||
复杂度
1-2) 常数。
3,5) O(N) 次比较,其中 N 为 cont.size() 。
另外,调用 O(N) 次 value_type 的构造函数,其中 N 为 cont.size() 。
4) O(N) 次比较,其中 N 为 cont.size() 。
6-8) 常数。
9) O(N) 次比较,其中 N 为 cont.size() 。
另外,调用 O(N) 次 value_type 的构造函数,其中 N 为 cont.size() 。
10) O(N) 次比较,其中 N 为 cont.size() 。
11) 与 other 的大小成线性。
12) 常数。
13) O(N) 次比较,其中 N 为 cont.size() + std::distance(first, last) 。
另外,调用 O(N) 次 value_type 的构造函数,其中 N 为 cont.size() 。
14) O(N) 次比较,其中 N 为 cont.size() + std::distance(first, last) 。
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| DR | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| P0935R0 | C++11 | 默认构造函数和构造函数 (4) 曾为 explicit | 使之为隐式 |
示例
带定制比较器的示例
#include <iostream> #include <queue> #include <vector> #include <utility> using my_pair_t = std::pair<size_t,bool>; using my_container_t = std::vector<my_pair_t>; int main() { auto my_comp = [](const my_pair_t& e1, const my_pair_t& e2) { return e1.first > e2.first; }; std::priority_queue<my_pair_t, my_container_t, decltype(my_comp)> queue(my_comp); queue.push(std::make_pair(5, true)); queue.push(std::make_pair(3, false)); queue.push(std::make_pair(7, true)); std::cout << std::boolalpha; while(!queue.empty()) { const auto& p = queue.top(); std::cout << p.first << " " << p.second << "\n"; queue.pop(); } }
输出: