这是姆洋自主研发的heap头文件
将其录入IDE,并保存为heap.h,保存在存放C++头文件的文件夹里(我只知道Dev-C++是Dev-cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++),就可以用
1 #include <heap.h>
的方式调用了。
1 //copyright muyang_233@outlook.com
2 //in 2019.9.25 v1.0
3 //this is a new STL by muyang_233 ownself that is a heap,you can use this to make a heap instead of priority_queue.
4 template<typename T>
5 struct heap{
6 T H[1000001];
7 int head=0;
8 void push(T& x){
9 H[++head]=x;
10 int _h=head;
11 while(H[_h/2]>H[_h]){
12 T ___=H[_h/2];H[_h/2]=H[_h];H[_h]=___;
13 _h/=2;
14 }
15 }
16 T top(){
17 return H[1];
18 }
19 void pop(){
20 H[1]=H[head];
21 head--;
22 int p=1,t;
23 while(p*2<=head){
24 if (p*2+1>head||H[p*2]<=H[p*2+1]) t=p*2;
25 else t=p*2+1;
26 if (H[p]>H[t]){
27 T ___=H[t];H[t]=H[p];H[p]=___;
28 p=t;
29 }
30 else break;
31 }
32 }
33 bool empty(){
34 return !head;
35 }
36 int size(){
37 return head;
38 }
39 };
持续更新,请关注<姆洋自主研发头文件>分类。