-
Notifications
You must be signed in to change notification settings - Fork 0
/
stlHeap.cpp
46 lines (34 loc) · 1.03 KB
/
stlHeap.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
46
/*
@Chiranjeevi
Date: 28-Jun-17
*/
/*
making a heap , deleting from it and adding , sorting heap
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct compare{
bool operator()(int a, int b){
if(a>b)
return true;
else
return false;
}
};
int main(){
vector<int> vec = {1, 7, 3, 6, 5, 4, 2, 9, 8, 10};
make_heap(vec.begin(), vec.end(), compare());
cout<<vec.front()<<endl;
pop_heap(vec.begin(), vec.end(), compare());
vec.pop_back(); //important otherwise remains at the endl
cout<<vec.front()<<endl;
vec.push_back(1);
push_heap(vec.begin(), vec.end(), compare());
cout<<vec.front()<<endl;
sort_heap(vec.begin(), vec.end(), compare()); //compare must be given otherwise behaves erratically if
//earlier given now not given (give same one)
for(int i: vec)
cout<<i<<" ";
}