Heap in C++ STL

Last Updated : 5 Jun, 2026

A heap is a specialized tree-based data structure that follows the heap property, where the highest-priority element is always stored at the root. In C++, the STL provides heap algorithms to efficiently create and manage heaps using containers such as vector.

  • By default, STL heap algorithms create a max heap.
  • Supports efficient insertion, deletion, and access to the highest-priority element.
  • Commonly used in priority queues, Heap Sort, and graph algorithms.

Heap Operations in C++ STL

The STL provides the following functions for working with heaps:

make_heap() Function

The std::make_heap() function is used to convert the given range in a container to a heap. By default, it generates the max heap but we can use a custom comparator to change it to the min heap.

Syntax

std::make_heap(begin_iterator, end_iterator);

Note: The provided iterators must be of the RandomAccessIterator type.

The following example demonstrates how to convert a vector into a heap using make_heap().

C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
    // Initializing a vector
    vector<int> v1 = { 20, 30, 40, 25, 15 };

    // Converting vector into a heap
    // using make_heap()
    make_heap(v1.begin(), v1.end());

    // Displaying the maximum element of heap
    // using front()
    cout << "The maximum element of heap is : ";
    cout << v1.front() << endl;

    return 0;
}

Output
The maximum element of heap is : 40

Explanation: The vector is converted into a max heap using make_heap(). In a max heap, the largest element is placed at the front of the container, which is accessed using front() and displayed on the screen.

Complexity analysis

Time Complexity: O(N), where N is the number of elements.
Auxiliary Space: O(1),since no extra space is used.

push_heap() Function

The std::push_heap() function is used to sort the heap after the insertion of an element at the end of the heap. We use the push_back() function of std::vector class to insert a new element at the end of the vector then use the push_heap() function to place that element at its appropriate position.

Syntax

std::push_heap(begin_interator, end_iterator);

C++
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

void print(vector<int>& vc)
{
    for (auto i : vc) {
        cout << i << " ";
    }
    cout << endl;
}

int main()
{
    vector<int> vc{ 20, 30, 40, 10 };

    make_heap(vc.begin(), vc.end());
    cout << "Initial Heap: ";
    print(vc);

    vc.push_back(50);
    cout << "Heap just after push_back(): ";
    print(vc);
    push_heap(vc.begin(), vc.end());
    cout << "Heap after push_heap(): ";
    print(vc);

    return 0;
}

Output
Initial Heap: 40 30 20 10 
Heap just after push_back(): 40 30 20 10 50 
Heap after push_heap(): 50 40 20 10 30 

Explanation: The vector is first converted into a heap using make_heap(). After inserting 50 with push_back(), the heap property is no longer maintained. Calling push_heap() rearranges the elements and places 50 at its correct position, restoring the max-heap structure.

Time Complexity: O(logN), where N is the number of elements.

Note: The push_heap() function should only be used after the insertion of a single element at the back. This function behavior is undefined if used for random insertion or to build a heap. For these cases, make_heap() function should be used.

pop_heap() Function

The std::pop_heap() function is used to move the largest element at the end of the heap so that we can safely remove the element without destroying the heap. Then we use the pop_back() function of std::vector class to actually remove that element.

Syntax

std::pop_heap(begin_iterator, end_iterator);

C++
#include <bits/stdc++.h>
using namespace std;

void print(vector<int>& vc)
{
    for (auto i : vc) {
        cout << i << " ";
    }
    cout << endl;
}

int main()
{
    // initial vector
    vector<int> vc{ 40, 10, 20, 50, 30 };

    // making heap
    make_heap(vc.begin(), vc.end());
    cout << "Initial Heap: ";
    print(vc);
  
    // using pop_heap() function to move the largest element
    // to the end
    pop_heap(vc.begin(), vc.end());
    cout << "Heap after pop_heap(): ";
    print(vc);

    // actually removing the element from the heap using
    // pop_back()
    vc.pop_back();
    cout << "Heap after pop_back(): ";
    print(vc);

    return 0;
}

Output
Initial Heap: 50 40 20 10 30 
Heap after pop_heap(): 40 30 20 10 50 
Heap after pop_back(): 40 30 20 10 

Explanation: The vector is first converted into a max heap using make_heap(). Calling pop_heap() moves the largest element to the end of the vector and adjusts the remaining elements to maintain the heap property. The element is then removed from the container using pop_back().

Time Complexity: O(logN), where N is the number of elements.

Note: pop_heap() only reorders the heap and does not delete any element. Use pop_back() after pop_heap() to remove the element from the container.

sort_heap()

The std::sort_heap() function is used to sort the heap in ascending order. It uses the heapsort algorithm and only works on the heap.

C++
#include <bits/stdc++.h>
using namespace std;
int main()
{

    // Initializing a vector
    vector<int> v1 = { 20, 30, 40, 25, 15 };

    // Converting vector into a heap
    // using make_heap()
    make_heap(v1.begin(), v1.end());

    // Displaying heap elements
    cout << "The heap elements are: ";
    for (int& x : v1)
        cout << x << " ";
    cout << endl;

    // sorting heap using sort_heap()
    sort_heap(v1.begin(), v1.end());

    // Displaying heap elements
    cout << "The heap elements after sorting are: ";
    for (int& x : v1)
        cout << x << " ";

    return 0;
}

Output
The heap elements are: 40 30 20 25 15 
The heap elements after sorting are: 15 20 25 30 40 

Explanation: The vector is first converted into a max heap using make_heap(). Calling sort_heap() rearranges the heap elements into ascending order, resulting in a sorted sequence.

Time Complexity: O(NlogN), where N is the number of elements.

Note: After calling sort_heap(), the range is no longer a heap and becomes a sorted sequence.

is_heap() Function

The std::is_heap() function is used to check whether the given range of the container is a heap or not. By default, it checks for max heap but we can also use a comparator to make it work for min heap.

Syntax

std::is_heap(begin_iterator, end_iterator);

Return Value

  • true if the given range is max_heap.
  • false if the given range is not a max_heap.
C++
#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector<int> v1 = {40, 30, 20, 10, 15};

    if (is_heap(v1.begin(), v1.end()))
        cout << "The range is a heap";
    else
        cout << "The range is not a heap";

    return 0;
}

Output
The range is a heap

Explanation: The vector satisfies the max-heap property, so is_heap() returns true. If the elements do not satisfy the heap property, the function returns false.

Time Complexity: O(N), where N is the number of elements.

is_heap_until() Function

The std::is_heap_until() function returns the iterator to the position till the container is the heap.

Syntax

std::is_heap_until(begin_iterator, end_iterator);

Return Value: Iterator till which the container is a heap.

C++
#include <bits/stdc++.h>
using namespace std;
int main()
{

    // Initializing a vector
    vector<int> v1 = { 40, 30, 25, 35, 15 };

    // Declaring heap iterator
    vector<int>::iterator it1;

    // Checking if container is heap
    // using is_heap()
    is_heap(v1.begin(), v1.end())
        ? cout << "The container is heap "  
        : cout << "The container is not heap"; // ternary operator
    cout << endl;

    // using is_heap_until() to check position
    // till which container is heap
    auto it = is_heap_until(v1.begin(), v1.end());

    // Displaying heap range elements
    cout << "The heap elements in container are : ";
    for (it1 = v1.begin(); it1 != it; it1++)
        cout << *it1 << " ";

    return 0;
}

Output
The container is not heap
The heap elements in container are : 40 30 25 

Explanation: The program first checks whether the entire vector is a heap using is_heap(). It then uses is_heap_until() to find the point where the heap property is violated and prints all elements that belong to the valid heap portion of the container.

Time Complexity: O(N), where N is the number of elements. 

Note: If the entire range satisfies the heap property, is_heap_until() returns the end iterator of the container.

Comment