-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheap.cpp
43 lines (34 loc) · 831 Bytes
/
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
41
42
43
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool func(int a, int b)
{
// for min heap use <
return a < b;
}
int main()
{
vector<int> v {4, 2, 5, 1, 7, 4, 6, 3, 9, 0};
// sort v as per max heap
make_heap(v.begin(), v.end(), func);
// max will be at the front
cout << v.front() << endl;
// sends the max (front element) to last position
pop_heap (v.begin(), v.end(), func);
// delete that number
v.pop_back();
// show current greatest number
cout << v.front() << endl;
// add 100 to v
v.push_back(100);
// sort in heap
push_heap (v.begin(), v.end(), func);
cout << v.front() << endl;
for (int i: v)
{
cout << i << " ";
}
cout << endl;
return 0;
}