std::ranges::pop_heap
來自 cppreference.com
定義於標頭檔案 <algorithm> |
||
呼叫簽名 (Call signature) |
||
template< std::random_access_iterator I, std::sentinel_for<I> S, class Comp = ranges::less, class Proj = std::identity > |
(1) | (C++20 起) |
template< ranges::random_access_range R, class Comp = ranges::less, class Proj = std::identity > |
(2) | (C++20 起) |
此函式交換給定堆的第一個元素和最後一個元素(根據 comp 和 proj),並將除了第一個位置之外的子範圍轉換為一個堆(根據 comp 和 proj)。這實際上是從給定堆中移除第一個元素。
1) 給定的堆是
[
first,
last)
。2) 給定的堆是 r。
本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即
目錄 |
[編輯] 引數
first, last | - | 定義要修改的元素範圍的迭代器-哨兵對 |
r | - | 要修改的元素範圍 |
comp | - | 應用於投影元素的比較器 |
proj | - | 應用於元素的投影 |
[編輯] 返回值
1) last
2) ranges::end(r)
[編輯] 複雜度
最多 2log(N) 次 comp 的應用,以及 4log(N) 次 proj 的應用,其中 N 是
1) ranges::distance(first, last)
2) ranges::distance(r)
[編輯] 示例
執行此程式碼
#include <algorithm> #include <array> #include <iostream> #include <iterator> #include <string_view> template<class I = int*> void print(std::string_view rem, I first = {}, I last = {}, std::string_view term = "\n") { for (std::cout << rem; first != last; ++first) std::cout << *first << ' '; std::cout << term; } int main() { std::array v{3, 1, 4, 1, 5, 9, 2, 6, 5, 3}; print("initially, v: ", v.cbegin(), v.cend()); std::ranges::make_heap(v); print("make_heap, v: ", v.cbegin(), v.cend()); print("convert heap into sorted array:"); for (auto n {std::ssize(v)}; n >= 0; --n) { std::ranges::pop_heap(v.begin(), v.begin() + n); print("[ ", v.cbegin(), v.cbegin() + n, "] "); print("[ ", v.cbegin() + n, v.cend(), "]\n"); } }
輸出
initially, v: 3 1 4 1 5 9 2 6 5 3 make_heap, v: 9 6 4 5 5 3 2 1 1 3 convert heap into sorted array: [ 6 5 4 3 5 3 2 1 1 9 ] [ ] [ 5 5 4 3 1 3 2 1 6 ] [ 9 ] [ 5 3 4 1 1 3 2 5 ] [ 6 9 ] [ 4 3 3 1 1 2 5 ] [ 5 6 9 ] [ 3 2 3 1 1 4 ] [ 5 5 6 9 ] [ 3 2 1 1 3 ] [ 4 5 5 6 9 ] [ 2 1 1 3 ] [ 3 4 5 5 6 9 ] [ 1 1 2 ] [ 3 3 4 5 5 6 9 ] [ 1 1 ] [ 2 3 3 4 5 5 6 9 ] [ 1 ] [ 1 2 3 3 4 5 5 6 9 ] [ ] [ 1 1 2 3 3 4 5 5 6 9 ]
[編輯] 參閱
(C++20) |
向一個最大堆新增一個元素 (演算法函式物件) |
(C++20) |
檢查給定的範圍是否是一個最大堆 (演算法函式物件) |
(C++20) |
尋找是一個最大堆的最大子範圍 (演算法函式物件) |
(C++20) |
從一個元素範圍建立一個最大堆 (演算法函式物件) |
(C++20) |
將一個最大堆轉換成一個按升序排序的元素範圍 (演算法函式物件) |
從一個最大堆中移除最大的元素 (函式模板) |