- C++ Basics
- C++ Introduction
- C++ Installation
- C++ Syntax
- C++ Hello World
- C++ Comments
- C++ Variables
- C++ Data Types
- C++ Constants
- C++ Type Casting
- C++ Inline
- C++ File Inclusion
- C++ Date & Time
- C++ Return Types
- C++ Object Oriented
- C++ Classes
- C++ Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Exceptions
- C++ Advanced
- C++ Conditions
- C++ Loops
- C++ Functions
- C++ Structures
- C++ Enums
- C++ References
- C++ Pointers
- C++ Data Structures
- C++ Libs
- C++ Data Structures
- C++ Arrays
- C++ Vectors
- C++ Lists
- C++ Linked List
- C++ Deque
- C++ Stacks
- C++ Queues
- C++ Priority Queues
- C++ Sets
- C++ Maps
- C++ Unordered Sets and Maps
- C++ Graphs
C++ Priority Queues
Priority queues in C++ are data structures that allow you to efficiently retrieve the element with the highest (or lowest) priority. The standard library provides a priority_queue container adapter that is built on top of a heap. By default, it implements a max-heap, where the largest element has the highest priority.
Here's a quick overview of how to use std::priority_queue:
Basic Usage
Include the necessary headers:
cpp#include#include Create a
priority_queue:By default, it uses a max-heap (largest element has the highest priority).
cppstd::priority_queue<int> pq;Add elements to the priority queue:
cpppq.push(10); pq.push(20); pq.push(15);Access the top element (highest priority):
cppstd::cout << "Top element: " << pq.top() << std::endl; // Outputs 20Remove the top element:
cpppq.pop();Check if the priority queue is empty:
cppif (pq.empty()) { std::cout << "Priority queue is empty." << std::endl; }
Custom Comparator
You can also use a custom comparator to change the behavior of the priority queue. For example, to create a min-heap (where the smallest element has the highest priority), you can use the following approach:
Include the necessary headers:
cpp#include#include #include Define a custom comparator:
cppstruct Compare { bool operator()(int a, int b) { return a > b; // Min-heap (smallest element has the highest priority) } };Create a priority queue with the custom comparator:
cppstd::priority_queue<int, std::vector<int>, Compare> pq;Add elements and use the priority queue as before:
cpppq.push(10); pq.push(20); pq.push(15); std::cout << "Top element: " << pq.top() << std::endl; // Outputs 10
Example Code
Here's a complete example demonstrating both the default max-heap and a custom min-heap:
cpp#include
#include
#include
// Custom comparator for a min-heap
struct Compare {
bool operator()(int a, int b) {
return a > b; // Min-heap
}
};
int main() {
// Default max-heap
std::priority_queue<int> maxHeap;
maxHeap.push(10);
maxHeap.push(20);
maxHeap.push(15);
std::cout << "Max-Heap:" << std::endl;
while (!maxHeap.empty()) {
std::cout << maxHeap.top() << " ";
maxHeap.pop();
}
std::cout << std::endl;
// Custom min-heap
std::priority_queue<int, std::vector<int>, Compare> minHeap;
minHeap.push(10);
minHeap.push(20);
minHeap.push(15);
std::cout << "Min-Heap:" << std::endl;
while (!minHeap.empty()) {
std::cout << minHeap.top() << " ";
minHeap.pop();
}
std::cout << std::endl;
return 0;
}
This code will output:
makefileMax-Heap:
20 15 10
Min-Heap:
10 15 20
Feel free to adjust the example based on your specific needs!