std::priority_queue
來自 cppreference.com
定義於標頭檔案 <queue> |
||
template< class T, |
||
優先順序佇列是一個容器介面卡,它提供以常數時間查詢最大(預設)元素,但代價是插入和提取操作為對數時間。
可以提供使用者定義的 Compare
以改變排序方式,例如,使用 std::greater<T> 將導致最小元素顯示為 top()。
使用 priority_queue
類似於在某個隨機訪問容器中管理堆,優點是不會意外地使堆失效。
|
(C++26 起) |
目錄 |
[編輯] 模板引數
T | - | 儲存元素的型別。如果 T 與 Container::value_type 不是同一型別,則程式格式錯誤。 |
Container(容器) | - | 用於儲存元素的底層容器的型別。容器必須滿足SequenceContainer的要求,並且其迭代器必須滿足LegacyRandomAccessIterator的要求。此外,它必須提供具有通常語義的以下函式
標準容器 std::vector(包括 |
Compare | - | 提供嚴格弱排序的 Compare 型別。 請注意,Compare 引數的定義使得如果它的第一個引數在弱排序中出現在第二個引數“之前”,它返回 true。但由於優先順序佇列首先輸出最大的元素,所以“在之前”的元素實際上是最後輸出的。也就是說,佇列的頭部包含根據 Compare 所施加的弱排序的“最後”元素。 |
[編輯] 成員型別
成員型別 | 定義 |
container_type
|
Container |
value_compare
|
Compare
|
value_type
|
Container::value_type |
size_type
|
Container::size_type |
reference
|
Container::reference |
const_reference
|
Container::const_reference |
[編輯] 成員物件
成員名稱 (Member name) | 定義 |
Container c |
底層容器 (protected 成員物件) |
Compare comp |
比較函式物件 (protected 成員物件) |
[編輯] 成員函式
構造 priority_queue (public 成員函式) | |
析構 priority_queue (public 成員函式) | |
向容器介面卡賦值 (public 成員函式) | |
元素訪問 | |
訪問頂部元素 (public 成員函式) | |
容量 | |
檢查容器介面卡是否為空 (public 成員函式) | |
返回元素數量 (public 成員函式) | |
修改器 | |
插入元素並對底層容器排序 (public 成員函式) | |
(C++23) |
插入一個元素範圍並對底層容器排序 (public 成員函式) |
(C++11) |
就地構造元素並對底層容器排序 (public 成員函式) |
移除頂部元素 (public 成員函式) | |
(C++11) |
交換內容 (public 成員函式) |
[編輯] 非成員函式
特化 std::swap 演算法 (函式模板) |
[編輯] 輔助類
特化 std::uses_allocator 型別特性 (類模板特化) | |
std::priority_queue 的格式化支援(類模板特化) |
推導指引 |
(C++17 起) |
[編輯] 注意
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_containers_ranges |
202202L |
(C++23) | 支援 Range 的容器構造和插入 |
__cpp_lib_constexpr_containers |
202502L |
(C++26) | constexpr std::priority_queue |
[編輯] 示例
執行此程式碼
#include <functional> #include <iostream> #include <queue> #include <string_view> #include <vector> template<typename T> void pop_println(std::string_view rem, T& pq) { std::cout << rem << ": "; for (; !pq.empty(); pq.pop()) std::cout << pq.top() << ' '; std::cout << '\n'; } template<typename T> void println(std::string_view rem, const T& v) { std::cout << rem << ": "; for (const auto& e : v) std::cout << e << ' '; std::cout << '\n'; } int main() { const auto data = {1, 8, 5, 6, 3, 4, 0, 9, 7, 2}; println("data", data); std::priority_queue<int> max_priority_queue; // Fill the priority queue. for (int n : data) max_priority_queue.push(n); pop_println("max_priority_queue", max_priority_queue); // std::greater<int> makes the max priority queue act as a min priority queue. std::priority_queue<int, std::vector<int>, std::greater<int>> min_priority_queue1(data.begin(), data.end()); pop_println("min_priority_queue1", min_priority_queue1); // Second way to define a min priority queue. std::priority_queue min_priority_queue2(data.begin(), data.end(), std::greater<int>()); pop_println("min_priority_queue2", min_priority_queue2); // Using a custom function object to compare elements. struct { bool operator()(const int l, const int r) const { return l > r; } } customLess; std::priority_queue custom_priority_queue(data.begin(), data.end(), customLess); pop_println("custom_priority_queue", custom_priority_queue); // Using lambda to compare elements. auto cmp = [](int left, int right) { return (left ^ 1) < (right ^ 1); }; std::priority_queue<int, std::vector<int>, decltype(cmp)> lambda_priority_queue(cmp); for (int n : data) lambda_priority_queue.push(n); pop_println("lambda_priority_queue", lambda_priority_queue); }
輸出
data: 1 8 5 6 3 4 0 9 7 2 max_priority_queue: 9 8 7 6 5 4 3 2 1 0 min_priority_queue1: 0 1 2 3 4 5 6 7 8 9 min_priority_queue2: 0 1 2 3 4 5 6 7 8 9 custom_priority_queue: 0 1 2 3 4 5 6 7 8 9 lambda_priority_queue: 8 9 6 7 4 5 2 3 0 1
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 307 | C++98 | Container 不能是 std::vector<bool> |
允許 |
LWG 2566 | C++98 | 缺少 Container::value_type 的要求 |
如果 T 與 Container::value_type 不是同一型別,則格式錯誤 |
LWG 2684 | C++98 | priority_queue 接受比較器但缺少其成員 typedef |
已新增 |
[編輯] 另請參閱
可變大小的連續陣列 (類模板) | |
節省空間的動態位集 (類模板特化) | |
雙端佇列 (類模板) |