This context is similar to a heap where only the max heap element can be retrieved (the one at the top in the priority queue) and elements can be inserted indefinitely.
Priority queues are implemented as container adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are popped from the "back" of the specific container, which is known as the top of the priority queue.
The underlying container may be any of the standard container class templates or some other specifically designed container class. The only requirement is that it must be accessible through random access iterators and it must support the following operations:
- front()
- push_back()
- pop_back()
Therefore, the standard container class templates vector and deque can be used. By default, if no container class is specified for a particular priority_queue class, the standard container class template vector is used.
Support for random access iterators is required to keep a heap structure internally at all times. This is done automatically by the container adaptor by calling the algorithms make_heap, push_heap and

本文介绍了C++ STL中的优先级队列priority_queue,它是一个基于堆结构的容器适配器。文章详细阐述了priority_queue的成员函数,包括push、pop和top等,以及其默认使用的底层容器vector。同时,讨论了如何通过指定容器类型和比较函数来定制优先级队列。
最低0.47元/天 解锁文章
1411

被折叠的 条评论
为什么被折叠?



