-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority_queue_usage.cpp
More file actions
42 lines (34 loc) · 1.18 KB
/
priority_queue_usage.cpp
File metadata and controls
42 lines (34 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <queue>
using namespace std;
// Define a struct representing your data
struct Data {
int priority;
string name;
// Constructor
Data(int p, const string& n) : priority(p), name(n) {}
// Overload the < operator for priority comparison
bool operator<(const Data& other) const {
return priority < other.priority;
}
};
int main() {
// Define a priority queue of Data structs
priority_queue<Data> pq; //max-heap
//can use multiply by -1 logic for minheap abilities
//or you can priority_queue <Data, vector<Data>, greater<Data>> gq;
//or better yet what chatgpt said (first time using it for this alr)
//to use priority_queue<Data> pq;
//and replace priority < other.priority with priority > other.priority
// Push some elements into the priority queue
pq.push(Data(3, "Alice"));
pq.push(Data(1, "Bob"));
pq.push(Data(2, "Charlie"));
// Pop elements from the priority queue
while (!pq.empty()) {
Data d = pq.top();
cout << "Name: " << d.name << ", Priority: " << d.priority << endl;
pq.pop();
}
return 0;
}