std::priority_queue<T,Container,Compare>::emplace
來自 cppreference.com
template< class... Args > void emplace( Args&&... args ); |
(C++11 起) | |
將一個新元素推入優先佇列。該元素在原地構造,即不執行復制或移動操作。元素的建構函式將以與提供給函式完全相同的引數呼叫。
實際上呼叫c.emplace_back(std::forward<Args>(args)...); std::push_heap(c.begin(), c.end(), comp);
目錄 |
[edit] 引數
args | - | 轉發給元素建構函式的引數 |
[edit] 返回值
(無)
[edit] 複雜度
對數次比較,加上 Container::emplace_back 的複雜度。
[edit] 示例
執行此程式碼
#include <iostream> #include <queue> struct S { int id; S(int i, double d, std::string s) : id{i} { std::cout << "S::S(" << i << ", " << d << ", \"" << s << "\");\n"; } friend bool operator< (S const& x, S const& y) { return x.id < y.id; } }; int main() { std::priority_queue<S> queue; queue.emplace(42, 3.14, "C++"); std::cout << "id: " << queue.top().id << '\n'; }
輸出
S::S(42, 3.14, "C++") id = 42
[edit] 另請參閱
插入元素並對底層容器排序 (public member function) | |
移除頂部元素 (public member function) |