12
Level up your coding skills with the C++ Standard Template Library (STL): Priority Queue BY JOYJIT CHOUDHURY

Priority Queue

Embed Size (px)

Citation preview

Level up your coding skills with

the C++ Standard Template

Library (STL):

Priority Queue

BY JOYJIT CHOUDHURY

Priority Queue

A priority_queue is a container adaptor that provides

constant time lookup of the largest (by default) element,

at the expense of logarithmic insertion and extraction.

A user-provided compare can be supplied to change the

ordering of the elements

Defined in the header <queue>

Belongs to the namespace std

(remember : using namespace std)

Heap

priority_queue gives a programmer the functionality of a

heap implemented in the C++ Standard Library

By default it’s a Max Heap : O(1) lookup of the largest

element

Using std::greater<int> as Compare makes it

a Min Heap: O(1) lookup of the smallest element

Insertion of elements

push(), inserts a new element in the priority_queue.

Access the Top element top() returns a constant reference to the top element in the

priority_queue, in constant time

Remove the Top element

pop() removes the top element from the priority_queue,

reducing it’s size by one.

Size size() returns the number of elements in the priority_queue

Is it empty?

empty() returns true if the priority_queue is empty, else

returns false

That’s not all. There are many other functions

and techniques that could come in handy.

Read about them on cplusplus.com or

cppreference.com or somewhere else. Just

google it !