The reason of using `std::greater` for creating min heap via `priority_queue`

The logical argument is as follows std::priority_queue is a container adaptor; basic memory considerations make the back the preferred place for modifications (with pop_back() and push_back()) for sequence containers such as std::vector. the priority_queue primitives are based on std::make_heap (constructor), std::pop_heap + container::pop_back (priority_queue::pop) and on container::push_back + std::push_heap (priority_queue::push) pop_heap will take the front …

Read more

What’s faster: inserting into a priority queue, or sorting retrospectively?

Testing is the best way to answer this question for your specific computer architecture, compiler, and implementation. Beyond that, there are generalizations. First off, priority queues are not necessarily O(n log n). If you have integer data, there are priority queues which work in O(1) time. Beucher and Meyer’s 1992 publication “The morphological approach to …

Read more

When should I use make_heap vs. Priority Queue?

There’s no difference in terms of performance. std::priority_queue is just an adapter class that wraps the container and the very same heap-related function calls into a class. The specification of the std::priority_queue openly states that. By building a heap from an exposed std::vector and calling heap-related functions directly, you keep it open to the possibility …

Read more