Note, this article is not finished! You can help by editing this doc page.
Overview
- Simplified
- Detailed
template< class T, /* ... */ >
class priority_queue;
template<
class T,
class Container = std::vector<T>,
class Compare = std::less<typename Container::value_type>
>
class priority_queue;
Priority queue is a container adapter - it adapts a container by providing a new interface to it.
The new interface resembles a queue with a specified order that's not based on the order of insertion, but rather based on some specified relation between the elements.
Technical definition of a priority queue
std::priority_queue
Defined in | queue |
Template parameters
pub | T | The type of the stored elements.
uwaga The behaviour is undefined if |
pub | Container | The type of the underlying container used to store the elements. By default The container must satisfy SequenceContainer and it's iterators must satisfy LegacyRandomAccesIterator.
The standard containers |
pub | Compare | A Compare type providing a strict weak ordering. zanotuj The Compare parameter is defined such that it returns |
Type names
pub | container_type | Container |
pub | value_compare | Compare |
pub | value_type | Container::value_type |
pub | size_type | Container::size_type |
pub | reference | Container::reference |
pub | const_reference | Container::const_reference |
Member functions
pub | (constructors) | Constructs a |
pub | (destructor) | Destructs a |
pub | operator= | Assigns one priority queue to another. |
Element access
pub | top | Accesses the element at the top. |
Capacity
pub | empty | Returns true if the queue is empty, otherwise false . |
pub | size | Returns the number of elements in the queue. |
Modifiers
pub | push | Inserts an element and sorts the underlying container using Compare . |
pub | emplace (since C++11) | Constructs an element in-place and sorts the underlying container. |
pub | pop | Removes the top element. |
pub | swap (since C++11) | Swaps two queues. |
Member objects
prot | Container C | The underlying container. By default std::vector<T> . |
prot | Compare comp | The comparator object. |
Non-member functions
pub | std::swap (std::priority_queue) | An overload for a std::swap algorithm. |
Helper classes
pub | std::uses_allocator (std::priority_queue) | Specializes the std::uses_allocator type trait. |
Deduction guides (since C++17)
Click to expand
Examples
Basic manipulation
#include <iostream>
#include <queue>
int main()
{
std::priority_queue<int> q;
queue.push(1);
queue.push(2);
queue.push(3);
while (!queue.empty()) {
std::cout << queue.top() << '\n';
queue.pop();
}
}
3
2
1
Hover to see the original license.