std::priority_queue<T,Container,Compare>::push_range
來自 cppreference.com
template< container-compatible-range<value_type> R > void push_range( R&& rg ); |
(C++23 起) | |
將 rg 的每個元素的副本插入到 priority_queue
中,如同透過
- c.append_range(std::forward<R>(rg)) (如果這是一個有效的表示式,即底層容器 c 具有適當的
append_range
成員函式),或者 - ranges::copy(rg, std::back_inserter(c)) (否則)。
然後恢復堆屬性,如同透過 ranges::make_heap(c, comp)。插入後 ranges::is_heap(c, comp) 為 true。
範圍 rg 中的每個迭代器被解引用一次。
目錄 |
[編輯] 引數
rg | - | 一個容器兼容範圍,即一個元素可轉換為 T 的input_range |
[編輯] 複雜度
c.append_range 的複雜度加上 ranges::make_heap(c, comp) 的複雜度。
[編輯] 注意
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_containers_ranges |
202202L |
(C++23) | 範圍感知(Ranges-aware)構造和插入 |
[編輯] 示例
執行此程式碼
#include <initializer_list> #include <queue> #include <version> #ifdef __cpp_lib_format_ranges #include <print> using std::println; #else #define FMT_HEADER_ONLY #include <fmt/ranges.h> using fmt::println; #endif int main() { std::priority_queue<int> adaptor; const auto rg = {1, 3, 2, 4}; #ifdef __cpp_lib_containers_ranges adaptor.push_range(rg); #else for (int e : rg) adaptor.push(e); #endif println("{}", adaptor); }
輸出
[4, 3, 2, 1]
[編輯] 參見
插入元素並對底層容器排序 (public member function) |