std::priori...ue::emplace(3)
π NAME
std::priority_queue::emplace β like, the emplace of std::priority_queue, bestie π
(okay so this page lives in the C++ Standard Libary manual β yes, that's their spelling up top, not mine, don't come for me π β and it's the std::priori...ue::emplace(3) page, the title got cutely truncated by the header, love that for it β¨)
β¨ SYNOPSIS β¨
template< class... Args > (since C++11)
void emplace( Args&&... args );
Okay so real talk: this bad boy pushes a new element to the priority queue. And the way it does it? The element is constructed in-place, which literally means no copy or move operations are performed β none, zero, nada πΈ. The constructor of the element gets called with exactly the same arguments you handed to the function, so it's giving "build it right where it lives" main character energy π
Effectively calls β i.e. under the hood it's doing this exact thing:
c.emplace_back(std::forward<Args>(args)...);
std::push_heap(c.begin(), c.end(), comp);
Slay. That's the whole magic trick π
π PARAMETERS
argsβ the arguments to forward to the constructor of the element. Like, you're basically handing the element's constructor its little starter pack π«Ά
πΈ RETURN VALUE
(none) β nothing, nope, void, she returns nothing and honestly? iconic π
π¦ COMPLEXITY
Logarithmic number of comparisons π₯ β plus the complexity of Container::emplace_back, so budget for that too, iykyk.
π EXAMPLE
// Run this code
#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';
}
π₯ OUTPUT
S::S(42, 3.14, "C++")
id = 42
That's literally what the page says, I'm just the messenger ππ
π«Ά SEE ALSO
pushβ inserts element and sorts the underlying container (public member function)popβ removes the top element (public member function)
okay that's the whole page bestie, you ate that π source is http://cppreference.com, dated 2024.06.10 β mwah, go forth and emplace responsibly πβ¨π§
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms