SlideShare a Scribd company logo
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 !
Priority Queue

More Related Content

What's hot

What's hot (20)

Data structures
Data structuresData structures
Data structures
 
stack & queue
stack & queuestack & queue
stack & queue
 
Understanding the components of standard template library
Understanding the components of standard template libraryUnderstanding the components of standard template library
Understanding the components of standard template library
 
Arrays Data Structure
Arrays Data StructureArrays Data Structure
Arrays Data Structure
 
Stack Implementation
Stack ImplementationStack Implementation
Stack Implementation
 
Standard Library Functions
Standard Library FunctionsStandard Library Functions
Standard Library Functions
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
9 python data structure-2
9 python data structure-29 python data structure-2
9 python data structure-2
 
Vector
VectorVector
Vector
 
List in java
List in javaList in java
List in java
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
 
Array Presentation
Array PresentationArray Presentation
Array Presentation
 
Data Structure In C#
Data Structure In C#Data Structure In C#
Data Structure In C#
 
Collections and generics
Collections and genericsCollections and generics
Collections and generics
 
Presentation on Elementary data structures
Presentation on Elementary data structuresPresentation on Elementary data structures
Presentation on Elementary data structures
 
Data Structure -List Stack Queue
Data Structure -List Stack QueueData Structure -List Stack Queue
Data Structure -List Stack Queue
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 
Stack data structure in Data Structure using C
Stack data structure in Data Structure using C Stack data structure in Data Structure using C
Stack data structure in Data Structure using C
 

Viewers also liked

Algorithm: priority queue
Algorithm: priority queueAlgorithm: priority queue
Algorithm: priority queue
Tareq Hasan
 
Merge sort
Merge sortMerge sort
Merge sort
Kumar
 
Applications of queues ii
Applications of queues   iiApplications of queues   ii
Applications of queues ii
Tech_MX
 
Fifo
FifoFifo
Fifo
ghada
 

Viewers also liked (20)

SPOJ Problem: OLOLO
SPOJ Problem: OLOLOSPOJ Problem: OLOLO
SPOJ Problem: OLOLO
 
Maps
MapsMaps
Maps
 
Sets
SetsSets
Sets
 
Algorithm: priority queue
Algorithm: priority queueAlgorithm: priority queue
Algorithm: priority queue
 
Heaps & priority queues
Heaps & priority queuesHeaps & priority queues
Heaps & priority queues
 
Priority queue
Priority queuePriority queue
Priority queue
 
Priority queues
Priority queuesPriority queues
Priority queues
 
computer notes - Priority queue
computer notes -  Priority queuecomputer notes -  Priority queue
computer notes - Priority queue
 
Heaps
HeapsHeaps
Heaps
 
Arduino uno-schematic
Arduino uno-schematicArduino uno-schematic
Arduino uno-schematic
 
23 priority queue
23 priority queue23 priority queue
23 priority queue
 
Algoritmo de ordenamiento: Heap Sort
Algoritmo de ordenamiento: Heap SortAlgoritmo de ordenamiento: Heap Sort
Algoritmo de ordenamiento: Heap Sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
deque and it applications
deque and it applicationsdeque and it applications
deque and it applications
 
FIFO Design
FIFO DesignFIFO Design
FIFO Design
 
3.4 selection sort
3.4 selection sort3.4 selection sort
3.4 selection sort
 
Merge sort: illustrated step-by-step walk through
Merge sort: illustrated step-by-step walk throughMerge sort: illustrated step-by-step walk through
Merge sort: illustrated step-by-step walk through
 
Applications of queues ii
Applications of queues   iiApplications of queues   ii
Applications of queues ii
 
Heap sort
Heap sort Heap sort
Heap sort
 
Fifo
FifoFifo
Fifo
 

Similar to Priority Queue

Assg 14 C++ Standard Template Library (STL)(Extra Credit .docx
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docxAssg 14 C++ Standard Template Library (STL)(Extra Credit .docx
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docx
festockton
 
Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...
Simplilearn
 
Educational Objectives After successfully completing this assignmen.pdf
Educational Objectives After successfully completing this assignmen.pdfEducational Objectives After successfully completing this assignmen.pdf
Educational Objectives After successfully completing this assignmen.pdf
rajeshjangid1865
 
Lab_3- Objective- Experiment with Lists- Stacks- and Queues- Simulate.docx
Lab_3- Objective- Experiment with Lists- Stacks- and Queues- Simulate.docxLab_3- Objective- Experiment with Lists- Stacks- and Queues- Simulate.docx
Lab_3- Objective- Experiment with Lists- Stacks- and Queues- Simulate.docx
rennaknapp
 
headerfilesinc-181121134545 (1).pdf
headerfilesinc-181121134545 (1).pdfheaderfilesinc-181121134545 (1).pdf
headerfilesinc-181121134545 (1).pdf
jazzcashlimit
 

Similar to Priority Queue (20)

C++ STL 概觀
C++ STL 概觀C++ STL 概觀
C++ STL 概觀
 
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docx
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docxAssg 14 C++ Standard Template Library (STL)(Extra Credit .docx
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docx
 
Ch03_stacks_and_queues.ppt
Ch03_stacks_and_queues.pptCh03_stacks_and_queues.ppt
Ch03_stacks_and_queues.ppt
 
James Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonJames Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on Python
 
Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...
 
Educational Objectives After successfully completing this assignmen.pdf
Educational Objectives After successfully completing this assignmen.pdfEducational Objectives After successfully completing this assignmen.pdf
Educational Objectives After successfully completing this assignmen.pdf
 
Header files in c
Header files in cHeader files in c
Header files in c
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
 
Functional programming in C++
Functional programming in C++Functional programming in C++
Functional programming in C++
 
Python for Beginners
Python  for BeginnersPython  for Beginners
Python for Beginners
 
Lab_3- Objective- Experiment with Lists- Stacks- and Queues- Simulate.docx
Lab_3- Objective- Experiment with Lists- Stacks- and Queues- Simulate.docxLab_3- Objective- Experiment with Lists- Stacks- and Queues- Simulate.docx
Lab_3- Objective- Experiment with Lists- Stacks- and Queues- Simulate.docx
 
headerfilesinc-181121134545 (1).pdf
headerfilesinc-181121134545 (1).pdfheaderfilesinc-181121134545 (1).pdf
headerfilesinc-181121134545 (1).pdf
 
Lec4
Lec4Lec4
Lec4
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
 
Study & Analysis of Complexities of Stack & Queue Operations in Data Structure
Study & Analysis of Complexities of Stack & Queue Operations in Data StructureStudy & Analysis of Complexities of Stack & Queue Operations in Data Structure
Study & Analysis of Complexities of Stack & Queue Operations in Data Structure
 
Educational slides by venay magen
Educational slides by venay magenEducational slides by venay magen
Educational slides by venay magen
 
Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional Programming
 
Object Oriented Programming Using C++: C++ STL Programming.pptx
Object Oriented Programming Using C++: C++ STL Programming.pptxObject Oriented Programming Using C++: C++ STL Programming.pptx
Object Oriented Programming Using C++: C++ STL Programming.pptx
 
16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 

Recently uploaded

Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfMastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
mbmh111980
 
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
Alluxio, Inc.
 

Recently uploaded (20)

Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfMastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
 
Breaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdfBreaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdf
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
GraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysisGraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysis
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning Framework
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in Michelangelo
 
iGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by SkilrockiGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by Skilrock
 
Top Mobile App Development Companies 2024
Top Mobile App Development Companies 2024Top Mobile App Development Companies 2024
Top Mobile App Development Companies 2024
 
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 
Agnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in KrakówAgnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in Kraków
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 

Priority Queue

  • 1. Level up your coding skills with the C++ Standard Template Library (STL): Priority Queue BY JOYJIT CHOUDHURY
  • 2. 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)
  • 3. 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
  • 4. Insertion of elements  push(), inserts a new element in the priority_queue.
  • 5. Access the Top element  top() returns a constant reference to the top element in the priority_queue, in constant time
  • 6. Remove the Top element  pop() removes the top element from the priority_queue, reducing it’s size by one.
  • 7. Size  size() returns the number of elements in the priority_queue
  • 8. Is it empty?  empty() returns true if the priority_queue is empty, else returns false
  • 9.
  • 10.
  • 11.  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 !