简体   繁体   English

C ++ STL优先级队列使用什么堆结构?

[英]What heap structure does C++ STL priority queue use?

Specifically, what heap variant does the STL priority queue container adaptor use? 具体来说,STL优先级队列容器适配器使用哪种堆变体? I'm benchmarking it vs my own hand rolled binary heap and double bucket structure implementations, so just wondering. 我正在使用我自己的手动滚动二进制堆和双桶结构实现进行基准测试,所以只是想知道。 Bonus points for any interesting implementation knowledge! 任何有趣的实施知识的奖励积分!

This question is tagged C++ (as opposed to asking for implementation-specific details on a particular compiler), so I've checked the standard for any information. 这个问题标记为C ++(而不是要求特定编译器的特定于实现的详细信息),因此我检查了标准以获取任何信息。 In various sections of 23.6.4 we learn that the priority_queue simply behaves as-if it uses make_heap , push_heap , and pop_heap . 在23.6.4的各个部分中,我们了解到priority_queue只是表现为 - 如果它使用make_heappush_heappop_heap Then these functions are documented (in 25.4.6 sections) as having complexity At most 3 * (last - first) comparisons. 然后记录这些函数(在25.4.6节中)具有复杂性At most 3 * (last - first) comparisons. , At most log(last - first) comparisons. At most log(last - first) comparisons. , and At most 2 * log(last - first) comparisons. At most 2 * log(last - first) comparisons. respectively. 分别。 So certain heap implementations may be indicated by these characteristics, but no specific heap is called out. 因此,某些堆实现可能由这些特性指示,但不会调出特定的堆。

You can choose the container underlying the priority_queue. 您可以选择priority_queue底层的容器。 If not specified the default is to use a std::vector: 如果未指定,则默认使用std :: vector:

template<
    class T,
    class Container = std::vector<T>,
    class Compare = std::less<typename Container::value_type>
> class priority_queue;

Container - The type of the underlying container to use to store the elements. Container - 用于存储元素的基础容器的类型。 The container must satisfy the requirements of SequenceContainer. 容器必须满足SequenceContainer的要求。 Additionally, it must provide the following functions with the usual semantics: front() push_back() pop_back() The standard containers std::vector and std::deque satisfy these requirements. 另外,它必须使用通常的语义提供以下函数:front()push_back()pop_back()标准容器std :: vector和std :: deque满足这些要求。

The heap is implemented by the *_heap functions, in a straightforward way. 堆由*_heap函数以直接的方式实现。 You can inspect the code here: http://www.sgi.com/tech/stl/stl_heap.h 您可以在此处查看代码: http//www.sgi.com/tech/stl/stl_heap.h

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM