Last Updated on December 14, 2022 by Prepbytes

### make_heap():

- make_heap() function is used to transform a sequence into a heap.
- Heap is a data structure in which we can place elements accordingly, and make them accessible in O(1) time.
- make_heap() is defined in the algorithm header file.
- We have two types of implementation of make_heap().

**1. make_heap(first_iterator, last_iterator)**

In the above syntax, the first iterator is the pointer where we have to start the sequence of elements to transform them into a heap.

In the last iterator, we stored a pointer to the next address to the last element of the sequence that has to be transformed into a heap.

#include#include // for heap #include using namespace std; int main() { // initializing vector; vector vi = { 4, 6, 7, 9, 11, 4 }; // using make_heap() to transform vector into // a max heap make_heap(vi.begin(),vi.end()); //checking if heap using // front() function cout << "The maximum element of the heap is: "; cout << vi.front() << endl; }

**OUTPUT:**

The maximum element of the heap is: 11

**2. make_heap(first_iterator, last_iterator, comp)**

In the above syntax, the first iterator is the pointer where we have to start the sequence of elements to transform them into a heap.

In the last iterator, we stored a pointer to the next address to the last element of the sequence that has to be transformed into the heap.

The comparator(comp) function returns a boolean value i.e. true or false of each element compared. Comp function will accept two arguments.

#include#include // for heap #include using namespace std; // comparator function to make min heap struct greaters{ bool operator()(const long& a,const long& b) const{ return a>b; } }; int main() { // initializing vector; vector vi = { 15, 6, 7, 9, 11, 45 }; // using make_heap() to transform vector into // a min heap make_heap(vi.begin(),vi.end(), greaters()); // checking if heap using // front() function cout << "The minimum element of the heap is: "; cout << vi.front() << endl; }

**OUTPUT:**

The minimum element of the heap is: 6

This article tried to discuss the **concept of make_heap() in C++ STL**. Hope this blog helps you understand the concept. To practice problems on Heap you can check out MYCODE | Competitive Programming – Heaps.