algorithm_is_heap

algorithm_is_heap (힙 검사)

std::is_heap는 대상 범위 [first, last)가 힙(heap) 성질을 만족하는지 검사해요. 최대 힙인지 확인하는 데 써요.

출처: cppreference

본문

std::is_heap는 대상 범위 [first, last)가 힙을 나타내는지 검사해요. <algorithm> 헤더에 정의되어 있어요.

template< class RandomIt >
bool is_heap( RandomIt first, RandomIt last );

template< class RandomIt, class Compare >
bool is_heap( RandomIt first, RandomIt last, Compare comp );
  • 1번 오버로드 — 힙 성질을 operator<(즉 std::less{})를 기준으로 검사해요.
  • 2번 오버로드 — 힙 성질을 비교 함수 comp를 기준으로 검사해요.
  • 병렬 실행 정책을 받는 오버로드도 있어요.

반환값 (Return value)

범위가 힙이면 true, 아니면 false예요.

복잡도 (Complexity)

Nstd::distance(first, last)라고 하면 최대 𝓞(N)번의 비교가 필요해요.

예제 (Example)

#include <algorithm>
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> v{9, 5, 4, 1, 1, 3};
    std::cout << std::boolalpha << std::is_heap(v.begin(), v.end()) << '\n';
    std::vector<int> v2{9, 4, 5, 1, 1, 3};
    std::cout << std::boolalpha << std::is_heap(v2.begin(), v2.end()) << '\n';
}

출력:

true
false

더 알아보기 (Learn more)

cppreference