std::sort_heap

Header: <algorithm>

Converts the heap [first,last) into a sorted range. The heap property is no longer maintained.

# Declarations

template< class RandomIt >
void sort_heap( RandomIt first, RandomIt last );

(constexpr since C++20)

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

(constexpr since C++20)

# Parameters

# Example

#include <algorithm>
#include <iostream>
#include <string_view>
#include <vector>
 
void println(std::string_view fmt, const auto& v)
{
    for (std::cout << fmt; const auto &i : v)
        std::cout << i << ' ';
    std::cout << '\n';
}
 
int main()
{
    std::vector<int> v{3, 1, 4, 1, 5, 9};
 
    std::make_heap(v.begin(), v.end());
    println("after make_heap, v: ", v);
 
    std::sort_heap(v.begin(), v.end());
    println("after sort_heap, v: ", v);
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 2444C++98at most (\scriptsize N \cdot \log(N))N⋅log(N) comparisons were allowedincreased to (\scriptsize 2N \cdot \log(N))2N⋅log(N)

# See also