Priority Queue

The std::priority_queue
is a reduced std::queue
. It needs the header <queue>
.
The difference to the std::queue
is, that their biggest element is always at the top of the priority queue. std::priority_queue pri
uses by default the comparison operator std::less
. Similar to std::queue
, pri.push(e)
inserts a new element e
into the priority queue. pri.pop()
removes the first element of the pri
, but does that with logarithmic complexity. With pri.top()
you can reference the first element in the priority queue, which is the greatest one. The std::priority_queue
knows its size, but didn’t support the comparison operator on their instances.
std::priority_queue
// priorityQueue.cpp
...
#include
<queue>
...
std
::
priority_queue
<
int
>
myPriorityQueue
;
std
::
cout
<<
myPriorityQueue
.
empty
()
<<
std
::
endl
;
// true
std
::
cout
<<
myPriorityQueue
.
size
()
<<
std
::
endl
;
// 0
myPriorityQueue
.
push
(
3
);
myPriorityQueue
.
push
...