问题描述
我有一个向量要用于创建堆.我不确定是否应该使用C ++ make_heap函数或将向量放在优先级队列中?在性能方面哪个更好?我什么时候应该使用另一种?
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?
推荐答案
性能差异没有区别. std::priority_queue
只是一个适配器类,它将容器和与堆相关的函数调用包装到一个类中. std::priority_queue
的规范公开指出了这一点.
There's no difference in therms 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.
通过从公开的std::vector
构建基于堆的优先级队列(通过直接调用与堆相关的函数),可以使它保持开放状态,以免外部访问,从而可能破坏堆/队列的完整性. std::priority_queue
用作限制访问规范"最小值的障碍:push()
,pop()
,top()
等.您可以将其视为自律实施措施.
By building a heap-based priority queue from an exposed std::vector
(by 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_queue
acts 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.
这篇关于什么时候应该使用make_heap vs. Priority Queue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!