Namespaces
Variants

std:: pop_heap

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
헤더 파일에 정의됨 <algorithm>
template < class RandomIt >
void pop_heap ( RandomIt first, RandomIt last ) ;
(1) (C++20부터 constexpr)
template < class RandomIt, class Compare >
void pop_heap ( RandomIt first, RandomIt last, Compare comp ) ;
(2) (C++20부터 constexpr)

first 위치의 값과 last - 1 위치의 값을 교환하고, [ first , last - 1 ) 부분 범위를 힙으로 만듭니다. 이는 [ first , last ) 에서 첫 번째 요소를 제거하는 효과를 가집니다.

1) [ first , last ) 는 다음에 대해 힙입니다 operator < (C++20 이전) std:: less { } (C++20 이후) .
2) [ first , last ) comp 에 대해 힙입니다.

다음 조건 중 하나라도 충족되면, 동작은 정의되지 않습니다:

  • [ first , last ) 가 비어 있습니다.
  • [ first , last ) 가 해당 비교자에 대한 힙이 아닙니다.
(C++11 이전)
(C++11 이후)

목차

매개변수

first, last - 루트 항목을 추출하기 위해 수정할 요소들의 비어 있지 않은 이진 힙 범위 를 정의하는 반복자 쌍
comp - 비교 함수 객체(즉, Compare 요구 사항을 만족하는 객체)로 첫 번째 인수가 두 번째 인수보다 작은 경우 true 를 반환합니다.

비교 함수의 시그니처는 다음에 해당해야 합니다:

bool cmp ( const Type1 & a, const Type2 & b ) ;

시그니처에 const & 가 필요하지는 않지만, 함수는 전달된 객체를 수정해서는 안 되며 값 범주 에 관계없이 (가능한 const인) Type1 Type2 타입의 모든 값을 수용할 수 있어야 합니다(따라서 Type1 & 는 허용되지 않으며 , Type1 Type1 에 대해 이동이 복사와 동등하지 않는 한 허용되지 않음 (C++11부터) ).
Type1 Type2 타입은 RandomIt 타입의 객체가 역참조된 후 두 타입 모두로 암시적으로 변환될 수 있어야 합니다.

타입 요구 사항
-
RandomIt LegacyRandomAccessIterator 요구 사항을 충족해야 합니다.
-
Compare Compare 요구 사항을 충족해야 합니다.

복잡도

주어진 N std:: distance ( first, last ) 인 경우:

1) 최대 2log(N) 번의 비교를 사용하며, operator < (C++20 이전) std:: less { } (C++20 이후) .
2) 최대 2log(N) 번의 비교 함수 comp 적용.

예제

#include <algorithm>
#include <iostream>
#include <string_view>
#include <type_traits>
#include <vector>
void println(std::string_view rem, const auto& v)
{
    std::cout << rem;
    if constexpr (std::is_scalar_v<std::decay_t<decltype(v)>>)
        std::cout << v;
    else
        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);
    std::pop_heap(v.begin(), v.end()); // moves the largest to the end
    println("after pop_heap:  ", v);
    int largest = v.back();
    println("largest element: ", largest);
    v.pop_back(); // actually removes the largest element
    println("after pop_back:  ", v);
}

출력:

after make_heap: 9 5 4 1 1 3
after pop_heap:  5 3 4 1 1 9
largest element: 9
after pop_back:  5 3 4 1 1

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 1205 C++98 [ first , last ) 구간이 비어있는 경우 동작이 명확하지 않았음 이 경우 동작은 정의되지 않음

참고 항목

최대 힙에 요소를 추가함
(함수 템플릿)
(C++11)
주어진 범위가 최대 힙인지 확인함
(함수 템플릿)
최대 힙인 가장 큰 부분 범위를 찾음
(함수 템플릿)
요소 범위로부터 최대 힙을 생성함
(함수 템플릿)
최대 힙을 오름차순으로 정렬된 요소 범위로 변환함
(함수 템플릿)
최대 힙에서 가장 큰 요소를 제거함
(알고리즘 함수 객체)