make_heap(): This function is build-in function in STL.This function is available in the file stl_heap.h. This function rearranges the elements in the range [first, last) in such a way that it forms a heap. By default, it forms a max heap. So the maximum value is always be the first element.It construct the heap over the given range.
Parameters: Two parameters are required for this function.
__first – Start of heap.
__last – End of heap.
Syntax:
make_heap(__first, __last)
For Example:
arr = {3, 2, 1, 4, 5, 10, 6}
make_heap(arr.begin(),arr.end()) = > It make heap 10 5 6 4 2 1 3.
Approach
C++
#include <bits/stdc++.h>using namespace std;int main(){vector<int> arr = {3, 2, 1, 4, 5, 10, 6};make_heap(arr.begin(), arr.end());for (int i = 0; i < arr.size(); i++)cout << arr[i] << " ";return 0;}
No comments:
Post a Comment