I have a vector that I want to use to create a heap. I’m not sure if I should use the C++ make_heap function or put my vector in a priority queue? Which is better in terms of performance? When should I use one vs. the other?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There’s no difference in terms of performance.
std::priority_queueis just an adapter class that wraps the container and the very same heap-related function calls into a class. The specification of thestd::priority_queueopenly states that.By building a
heapfrom an exposedstd::vectorand calling heap-related functions directly, you keep it open to the possibility of outside access, potentially damaging the integrity of the heap/queue.std::priority_queueacts as a barrier restricting that access to a "canonical" minimum:push(),pop(),top()etc. You can see it as self-discipline enforcing measure.Also, by adapting your queue interface to the "canonical" set of operations, you make it uniform and interchangeable with other class-based implementations of priority queues that conform to the same external specification.