+ 2

# Heaps(STL)

---Is there anything that I should change/add from this code for the required assignment? thanks A heap data structure can be efficiently implemented in a range using the C++ Standard Template Library (STL). In this exercise you are asked to utilize STL in order to build and manipulate a heap efficiently. In particular, you are requested to: 1. Make a heap consisting of 10 different integers of your own choosing. Include a statement to show the maximum element of the heap. 2. Add a new value that is the mean of the random values you creates in the previous step. Floor the value if needed (truncate decimal part). 3. Delete the maximum element of the heap and 4. Sort the heap. Write down the code to achieve all the operations mentioned above and clarify which section of the code does what. #include<iostream> #include <vector> #include<algorithm> #include <stdlib.h> using namespace std; int main() { vector<int> v1; int i; // Initialized a vector for(i=0;i<10;i++){ v1.push_back(rand()); } // Converting vector into a heap using make_heap() make_heap(v1.begin(), v1.end()); // Displaying heap elements cout << "The heap elements Before Sorting are : "; for (int &val : v1) cout << val << " "; cout << endl; // sorting heap using sort_heap() sort_heap(v1.begin(), v1.end()); cout << "The maximum element of heap that are deleted : "; cout << v1.front() << endl; //using pop_heap() to delete maximum element pop_heap(v1.begin(), v1.end()); v1.pop_back(); // Displaying heap elements after sorting cout << "The heap elements after sorting are : "; for (int &val : v1) cout << val << " "; return 0; }

13th Dec 2019, 1:02 AM
lakas