std::push_heap

Header: <algorithm>

Inserts the element at the position last - 1 into the heap [first,last - 1). The heap after the insertion will be [first,last).

# Declarations

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

(constexpr since C++20)

template< class RandomIt, class Compare >
void push_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 rem, const std::vector<int>& v)
{
    std::cout << rem;
    for (int e : v)
        std::cout << e << ' ';
    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.push_back(6);
    println("after push_back: ", v);
 
    std::push_heap(v.begin(), v.end());
    println("after push_heap: ", v);
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 3032C++98the elements of [first, last) was not required to be swappablerequired

# See also