std::priority_queue::pop(3)
π std::priority_queue::pop β like, from the C++ Standard Library, bestie (3)
Okay so picture this: it's std::priority_queue::pop(3), the glow-up page for one tiny member function. Section names stay the same because we're classy like that, but the vibes? Immaculate. Let's go. π«Ά
β¨ NAME β¨
std::priority_queue::pop β literally just std::priority_queue::pop. That's the whole name. Iconic, honestly. No notes. π
πΈ Synopsis πΈ
void pop();
This one right here is the bouncer at the club. It removes the top element from the priority queue, periodt. πͺβ¨
Under the hood it's effectively calling this exact combo:
std::pop_heap(c.begin(), c.end(), comp);
c.pop_back();
So yeah β shuffle the heap so the top one goes to the back, then yeet it off the end. That's the whole trick. π₯
π Parameters π
None bestie. It takes nothing. (none). Zero arguments. It's giving low-maintenance, high-impact. π
β Return value β
Also nothing. (none). It does the thing and leaves. No receipts, no return, just vibes and a slightly shorter queue. π
π¦ Complexity π¦
So the cost is a logarithmic number of comparisons, plus the complexity of Container::pop_back. Both of those matter β don't forget the second part, that's the sneaky one. π§
π Example π
Come on, let's run this code together:
// Run this code
#include <iostream>
#include <queue>
struct Event
{
int priority{};
char data{' '};
friend bool operator<(Event const& lhs, Event const& rhs)
{
return lhs.priority < rhs.priority;
}
friend std::ostream& operator<<(std::ostream& os, Event const& e)
{
return os << '{' << e.priority << ", '" << e.data << "'}";
}
};
int main()
{
std::priority_queue<Event> events;
std::cout << "Fill the events queue:\t";
for (auto const e : {Event{6,'L'}, {8,'I'}, {9,'S'}, {1,'T'}, {5,'E'}, {3,'N'}})
{
std::cout << e << ' ';
events.push(e);
}
std::cout << "\nProcess events:\t\t";
for (; !events.empty(); events.pop())
{
Event const& e = events.top();
std::cout << e << ' ';
}
std::cout << '\n';
}
And the Output? She ate, truly:
Fill the events queue: {6, 'L'} {8, 'I'} {9, 'S'} {1, 'T'} {5, 'E'} {3, 'N'}
Process events: {9, 'S'} {8, 'I'} {6, 'L'} {5, 'E'} {3, 'N'} {1, 'T'}
See how they go in all chaotic and come out all sorted by priority, highest first? That's the pop() doing its little dance in the loop condition. Main character energy fr fr. ππ
π See also π
emplaceβ constructs the element in-place and sorts the underlying container. (C++11) (public member function)pushβ inserts the element and sorts the underlying container. (public member function)topβ accesses the top element. (public member function)
Lowkey a public service announcement:pop()removes the top β it does not hand it to you. If you want to actually see what you're deleting, go grabtopfirst. That's the whole reason they're listed together, iykyk. π
And that's the page, bestie! Sourced straight from http://cppreference.com, dated 2024.06.10, for std::priority_queue::pop(3). Go forth and pop 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