C++ std::pop_heap() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

Definition

In C++, std::pop_heap() moves the largest element (the front) of a heap to the back of the range and fixes the rest so it is still a heap.

To really remove the element, call pop_back() on the vector afterwards.

Syntax

Add #include <algorithm>, then call std::pop_heap(). Start with these two lines.

C++
std::pop_heap(v.begin(), v.end());
v.pop_back();
CallWhat it does
std::pop_heap(v.begin(), v.end());Move the top element to v.back().
v.pop_back();Delete that element from the vector.
Library declarations
C++
template<class RandomIt>
void pop_heap(RandomIt first, RandomIt last);

template<class RandomIt, class Compare>
void pop_heap(RandomIt first, RandomIt last, Compare comp);

Parameters

ParameterDescription
first, lastRequired. A range that is already a heap.
compOptional. The comparison used to build the heap.

Return Value

Return ValueDescription
voidNothing is returned. Read the removed value from v.back().

C++ Examples

Example 1 — Remove the largest

Take the biggest value out of a heap.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {9, 5, 7, 1};
    std::make_heap(v.begin(), v.end());

    std::pop_heap(v.begin(), v.end());
    std::cout << "Removed: " << v.back() << '\n';
    v.pop_back();

    std::cout << "New largest: " << v.front() << '\n';
    return 0;
}

How It Works

  1. pop_heap() swaps the front with the last element and repairs the rest.
  2. The old largest value, 9, is now at the back, and pop_back() drops it.
  3. The next largest value, 7, is at the front.

Example 2 — Drain a heap

Pop until the heap is empty to read values from large to small.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {4, 8, 2, 6};
    std::make_heap(v.begin(), v.end());

    while (!v.empty()) {
        std::pop_heap(v.begin(), v.end());
        std::cout << v.back() << ' ';
        v.pop_back();
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. Each loop round moves the largest remaining value to the back.
  2. We print it and then remove it.
  3. The values come out in descending order, which is a heap sort.

Example 3 — Min-heap pop

Use std::greater to take out the smallest value.

C++
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {5, 3, 8};
    std::make_heap(v.begin(), v.end(), std::greater<int>());

    std::pop_heap(v.begin(), v.end(), std::greater<int>());
    std::cout << "Smallest: " << v.back() << '\n';
    v.pop_back();

    std::cout << "Next smallest: " << v.front() << '\n';
    return 0;
}

How It Works

  1. The heap is built with std::greater<int>(), so the smallest value is on top.
  2. The same comparison must be passed to pop_heap().
  3. 3 is removed first, and then 5 is on top.

Frequently Asked Questions

In C++, std::pop_heap() moves the top element of a heap to the end of the range. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns nothing (void). The top element moves to the last position and the rest stays a heap.
No. It only moves it to the back. Call v.pop_back() to remove it.
Pass the same comparison, such as std::greater<int>(), to make_heap(), push_heap() and pop_heap().

Next: prev_permutation()

Rearranges a range into the previous permutation in dictionary order.

prev_permutation() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful