Adapt a container to provide a priority queue
std::priority_queue
Defined in the header file <queue>
template< class T, |
priority_queue is a container adapter that provides constant-time (default) maximum element lookup, logarithmic cost insertion and extraction.
The order can be Compare
changed , eg with std::greater<T> will cause the smallest element to appear as top().
priority_queue
Works similarly to managing the heap in some random-access container, with the advantage that it is impossible to suddenly invalidate the heap.
template parameter
T | - | The stored element type. The behavior is undefined if T and Container::value_type is not the same type. (since C++17) |
Container | - | The underlying container type used to store elements. A container must meet the requirements of a sequence container (SequenceContainer), and its iterator must meet the requirements of a legacy random access iterator (LegacyRandomAccessIterator). Additionally, it must provide the following functions with the usual semantics:
The standard containers std::vector and std::deque meet these requirements. |
Compare | - | Provides a strict and weakly ordered comparison (Compare) type. |
Note that the Compare parameter is defined such that it returns true if its first argument precedes its second argument in weak ordering. But because priority_queue outputs the largest element first, the "first come" element is actually output last. That is, the head of the queue contains the "last" element in the weak ordering imposed by Compare.
member type
member type | definition |
container_type |
Container |
value_compare (C++17) |
Compare |
value_type |
Container::value_type |
size_type |
Container::size_type |
reference |
Container::reference |
const_reference |
Container::const_reference |
member function
(Constructor) |
Construct priority_queue (public member function) |
(destructor) |
Destructor priority_queue (public member function) |
operator= |
Assign to container adapter (public member function) |
element access |
|
top |
Access the top element of the stack (public member function) |
capacity |
|
empty |
Check if the underlying container is empty (public member function) |
size |
Returns the number of elements held (public member function) |
modifier |
|
push |
Inserts elements and sorts the underlying container (public member function) |
place (C++11) |
Constructs elements in-place and sorts the underlying container (public member function) |
pop |
Delete the top element of the stack (public member function) |
swap |
exchange content (public member function) |
member object |
|
Container c |
underlying container (protected member object) |
Compare comp |
comparison function object (protected member object) |
non-member function
std::swap(std::priority_queue) |
specialization of the std::swap algorithm (function template) |
Auxiliary class
std::uses_allocator<std::priority_queue> (C++11) |
特化 std::uses_allocator 类型特性 (函数模板) |