-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path52.cpp
55 lines (43 loc) · 1.44 KB
/
52.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
47
48
49
50
51
52
53
54
55
#include<iostream>
#include<algorithm>
#include<vector>
int main()
{
//program to understand heap data structure using STL container
//initializing a vector
std::vector<int> v = {3,7,63,45,12,89,123,45};
//using make_heap() function to convert the vector to a heap
std::make_heap(v.begin(),v.end());
//getting the max element of the heap
std::cout<<"Max element of the heap : "<<v.front()<<std::endl;
//adding more elements to the vector
v.push_back(900);
v.push_back(33);
//using push_heap() function to reorder elements
std::make_heap(v.begin(),v.end());
//max element of the heap
std::cout<<"Max element of the heap : "<<v.front()<<std::endl;
//pop_heap() to delete the max element of the heap
std::pop_heap(v.begin(),v.end());
v.pop_back();
std::cout<<"Max element of the heap : "<<v.front()<<std::endl;
//displaying the heap elements
std::cout<<"Heap elements : ";
for(int &i : v)
{
std::cout<<i<<" ";
}
std::cout<<"\n";
//using sort_heap() function
std::sort_heap(v.begin(),v.end());
//displaying the heap elements
std::cout<<"After sorting, Heap elements : ";
for(int &i : v)
{
std::cout<<i<<" ";
}
std::cout<<"\n";
//checking if container is heap
std::is_heap(v.begin(),v.end())?std::cout<<"Container is heap!"<<std::endl:std::cout<<"Container is not heap!!"<<std::endl;
return 0;
}