std::priority_queue::pop(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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 grab top first. 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