Namespaces
Variants

std::make_heap

cppreference.net에서
 
 
알고리즘 라이브러리
제약된 알고리즘 및 범위에 대한 알고리즘 (C++20)
제약된 알고리즘, 예: ranges::copy, ranges::sort, ...
분할 연산
partition
stable_partition    
partition_point    
정렬 연산

sort
is_sorted_until
nth_element

(분할된 범위에 대해)
lower_bound
includes
merge
push_heap
is_heap_until
최소/최대 연산
max
clamp
max_element
사전식 비교 연산

lexicographical_compare
순열 연산
next_permutation
실행 정책




 
template< class RandomIt >
void make_heap( RandomIt first, RandomIt last );
(C++20부터 constexpr) 범위
template< class RandomIt, class Compare >
void make_heap( RandomIt first, RandomIt last, Compare comp );
heap 을 만듭니다.

1) 생성된 힙은 [first, last)(C++20까지)

(C++20부터)에 대한 것입니다.operator<2)std::less{} 생성된 힙은 에 대한 것입니다.
다음 조건 중 하나라도 만족하면 동작이 정의되지 않습니다: comp의 타입이

이 아닌 경우.

참고 항목

first, last - 이진 힙 범위를 만들 요소의 범위
를 정의하는 반복자 쌍 comp - 비교 함수 객체 (즉, Comparetrue의 요구사항을 만족하는 객체)로, 를 반환합니다 (첫 번째 인수가 작으면

).

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

비교 함수의 서명은 다음과 같아야 합니다: const&서명에 Type1가 없어도 되지만, 함수는 전달된 객체를 수정하지 않아야 하며, (가능한 const) Type2 및 타입의 모든 값을 값 범주Type1&에 관계없이 받아들일 수 있어야 합니다 (따라서 은 허용되지 않으며, Type1도 Type1 이동이 복사와 동등한 경우가 아니면 허용되지 않습니다(C++11부터)
).Type1 타입 Type2과 RandomIt은

타입의 객체를 역참조한 후 두 타입 모두로 암시적으로 변환될 수 있어야 합니다.
타입 요구사항
RandomIt- LegacyRandomAccessIterator
의 요구사항을 충족해야 합니다.
Compare- Compare

의 요구사항을 충족해야 합니다.

복잡도\(\scriptsize N\)Nstd::distance(first, last)

: 최대 \(\scriptsize 3N\)operator<3Nstd::less{}번의 비교를 (C++20까지)
(C++20부터) 사용하여 수행합니다. 최대 \(\scriptsize 3N\)comp3N

번의 비교 함수

#include <algorithm>
#include <functional>
#include <iostream>
#include <string_view>
#include <vector>

void print(std::string_view text, const std::vector<int>& v = {})
{
    std::cout << text << ": ";
    for (const auto& e : v)
        std::cout << e << ' ';
    std::cout << '\n';
}

int main()
{
    print("Max heap");
    
    std::vector<int> v{3, 2, 4, 1, 5, 9};
    print("initially, v", v);
    
    std::make_heap(v.begin(), v.end());
    print("after make_heap, v", v);
    
    std::pop_heap(v.begin(), v.end());
    print("after pop_heap, v", v);
    
    auto top = v.back();
    v.pop_back();
    print("former top element", {top});
    print("after removing the former top element, v", v);
    
    print("\nMin heap");
    
    std::vector<int> v1{3, 2, 4, 1, 5, 9};
    print("initially, v1", v1);
    
    std::make_heap(v1.begin(), v1.end(), std::greater<>{});
    print("after make_heap, v1", v1);
    
    std::pop_heap(v1.begin(), v1.end(), std::greater<>{});
    print("after pop_heap, v1", v1);
    
    auto top1 = v1.back();
    v1.pop_back();
    print("former top element", {top1});
    print("after removing the former top element, v1", v1);
}

Max heap:
initially, v: 3 2 4 1 5 9
after make_heap, v: 9 5 4 1 2 3
after pop_heap, v: 5 3 4 1 2 9
former top element: 9
after removing the former top element, v: 5 3 4 1 2

Min heap:
initially, v1: 3 2 4 1 5 9
after make_heap, v1: 1 2 4 3 5 9
after pop_heap, v1: 2 3 4 9 5 1
former top element: 1
after removing the former top element, v1: 2 3 4 9 5

예제

이 코드 실행

출력: 결함 보고서 다음 동작 변경 결함 보고서는 이전에 발행된 C++ 표준에 소급 적용되었습니다.
DR 적용된 표준 발표된 동작[first, last)올바른 동작 LWG 3032

C++98

요소는 스와핑 가능할 필요가 없었습니다
필요함
&참고 항목
(C++11)
알고리즘 함수 객체)
ranges::is_heap &(C++20)
(함수 템플릿
알고리즘 함수 객체) &ranges::is_heap_until
push_heap
(함수 템플릿
알고리즘 함수 객체) &ranges::push_heap
pop_heap
(함수 템플릿
알고리즘 함수 객체) &ranges::pop_heap
sort_heap
(함수 템플릿
알고리즘 함수 객체)
priority_queue
컨테이너를 적응시켜 우선순위 큐를 제공합니다
(클래스 템플릿)