다음을 통해 공유


make_heap

Converts elements from a specified range into a heap in which the first element is the largest and for which a sorting criterion may be specified with a binary predicate.

template<class RandomAccessIterator> 
   void make_heap( 
      RandomAccessIterator _First,  
      RandomAccessIterator _Last 
   ); 
template<class RandomAccessIterator, class BinaryPredicate> 
   void make_heap( 
      RandomAccessIterator _First,  
      RandomAccessIterator _Last,
      BinaryPredicate _Comp 
   );

매개 변수

  • _First
    A random-access iterator addressing the position of the first element in the range to be converted into a heap.

  • _Last
    A random-access iterator addressing the position one past the final element in the range to be converted into a heap.

  • _Comp
    한 요소가 다른 요소보다 작은 개념을 정의하는 사용자 정의 조건자 함수 개체입니다. 이진 조건자는 두 개의 인수를 사용하고 만족할 때는 true를 만족하지 못할 때는 false를 반환합니다.

설명

Heaps have two properties:

  • The first element is always the largest.

  • Elements may be added or removed in logarithmic time.

Heaps are an ideal way to implement priority queues and they are used in the implementation of the Standard Template Library container adaptor priority_queue Class.

The complexity is linear, requiring 3 * (_Last – _First) comparisons.

예제

// alg_make_heap.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>

int main() {
   using namespace std;
   vector <int> v1, v2;
   vector <int>::iterator Iter1, Iter2;

   int i;
   for ( i = 0 ; i <= 9 ; i++ )
      v1.push_back( i );

   random_shuffle( v1.begin( ), v1.end( ) );

   cout << "Vector v1 is ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl;

   // Make v1 a heap with default less than ordering
   make_heap ( v1.begin( ), v1.end( ) );
   cout << "The heaped version of vector v1 is ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl;

   // Make v1 a heap with greater than ordering
   make_heap ( v1.begin( ), v1.end( ), greater<int>( ) );
   cout << "The greater-than heaped version of v1 is ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl;
}

샘플 출력

Vector v1 is ( 8 1 9 2 0 5 7 3 4 6 ).
The heaped version of vector v1 is ( 9 6 8 4 1 5 7 3 2 0 ).
The greater-than heaped version of v1 is ( 0 1 5 2 6 8 7 3 4 9 ).

요구 사항

헤더: <algorithm>

네임스페이스: std

참고 항목

참조

heap

표준 템플릿 라이브러리