-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpriority_queue_remove.cpp
45 lines (36 loc) · 963 Bytes
/
priority_queue_remove.cpp
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
43
44
45
#include <iostream>
#include <queue> //priority_queue
#include <algorithm> //find
//https://stackoverflow.com/questions/19467485/how-to-remove-element-not-at-top-from-priority-queue
template<typename T>
class custom_priority_queue : public std::priority_queue<T, std::vector<T>>{
public:
bool remove(const T& value) {
auto it = std::find(this->c.begin(), this->c.end(), value);
if (it != this->c.end()) {
this->c.erase(it);
std::make_heap(this->c.begin(), this->c.end(), this->comp);
return true;
} else {
return false;
}
}
};
int main() {
custom_priority_queue<int> pq;
pq.push(10);
pq.push(2);
pq.push(4);
pq.push(6);
pq.push(3);
pq.remove(6);
while (!pq.empty()) {
std::cout << pq.top();
pq.pop();
if (!pq.empty()){
std::cout << ", ";
}
}
return 0;
}
//10, 4, 3, 2