Namespaces
Variants

std:: is_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
is_heap
(C++11)
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
헤더 파일에 정의됨 <algorithm>
template < class RandomIt >
bool is_heap ( RandomIt first, RandomIt last ) ;
(1) (C++11부터)
(C++20부터 constexpr)
template < class ExecutionPolicy, class RandomIt >

bool is_heap ( ExecutionPolicy && policy,

RandomIt first, RandomIt last ) ;
(2) (C++17부터)
template < class RandomIt, class Compare >
bool is_heap ( RandomIt first, RandomIt last, Compare comp ) ;
(3) (C++11부터)
(C++20부터 constexpr)
template < class ExecutionPolicy, class RandomIt, class Compare >

bool is_heap ( ExecutionPolicy && policy,

RandomIt first, RandomIt last, Compare comp ) ;
(4) (C++17부터)

[ first , last ) 범위가 heap 인지 확인합니다.

1) 검사되는 힙 속성은 operator < (C++20 이전) std:: less { } (C++20 이후) 에 관한 것입니다.
3) 검사할 힙 속성은 comp 를 기준으로 합니다.
2,4) (1,3) 와 동일하지만, policy 에 따라 실행됩니다.
다음 모든 조건이 만족될 때만 이 오버로드들이 오버로드 해결에 참여합니다:

std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> true 입니다.

(C++20 이전)

std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> true 입니다.

(C++20 이후)

목차

매개변수

first, last - 검사할 요소들의 범위 를 정의하는 반복자 쌍
policy - 사용할 실행 정책
comp - 비교 함수 객체(즉 Compare 요구사항을 만족하는 객체)로 첫 번째 인수가 두 번째 인수보다 작은 경우 true 를 반환함.

비교 함수의 시그니처는 다음과 동일해야 함:

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

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

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

반환값

true 해당 비교자를 기준으로 범위가 힙인 경우, false 그렇지 않은 경우.

복잡도

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

1,2) O(N) 번의 비교를 사용하며 operator < (C++20 이전) std:: less { } (C++20 이후) 를 사용합니다.
3,4) O(N) 번의 비교 함수 comp 적용.

예외

ExecutionPolicy 라는 템플릿 매개변수를 사용하는 오버로드는 다음과 같이 오류를 보고합니다:

  • 알고리즘의 일부로 호출된 함수 실행 중 예외가 발생하고 ExecutionPolicy 표준 정책 중 하나인 경우, std::terminate 가 호출됩니다. 다른 ExecutionPolicy 의 경우 동작은 구현에 따라 정의됩니다.
  • 알고리즘이 메모리 할당에 실패하는 경우, std::bad_alloc 이 throw됩니다.

예제

#include <algorithm>
#include <bit>
#include <iostream>
#include <vector>
int main()
{
    std::vector<int> v{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9};
    std::cout << "initially, v:\n";
    for (const auto& i : v)
        std::cout << i << ' ';
    std::cout << '\n';
    if (!std::is_heap(v.begin(), v.end()))
    {
        std::cout << "making heap...\n";
        std::make_heap(v.begin(), v.end());
    }
    std::cout << "after make_heap, v:\n";
    for (auto t{1U}; const auto& i : v)
        std::cout << i << (std::has_single_bit(++t) ? " | " : " ");
    std::cout << '\n';
}

출력:

initially, v:
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9
making heap...
after make_heap, v:
9 | 6 9 | 5 5 9 7 | 1 1 3 5 8 3 4 2 |

참고 항목

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