-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path5.c++_heap.cpp
40 lines (37 loc) · 1006 Bytes
/
5.c++_heap.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
/*************************************************************************
> File Name: 5.c++_heap.cpp
> Author: rEn Chong
> Mail: [email protected]
> Created Time: 五 3/10 21:57:53 2023
************************************************************************/
#include <iostream>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
template<typename T>
class Heap : public vector<T> {
public :
template<typename Func_T>
Heap(Func_T cmp) : cmp(cmp) {}
void push(const T &a) {
this->push_back(a);
push_heap(this->begin(), this->end, cmp);
return ;
}
void pop(const T &a) {
pop_heap(this->begin(), this->end, cmp);
this->pop_back(a);
return ;
}
T &top() { return this->at(0); }
private :
std::function<bool(T, T)> cmp;
};
int main() {
Heap<int> h1{std::less<int>()};
// 传入< 代表小的优先级高 小顶堆
Heap<int> h2{std::greater<int>()};
return 0;
}