다음을 통해 공유


push_heap

범위에서 이전 요소로 구성 된 기존 힙 범위 끝에 요소를 추가 합니다.

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

매개 변수

  • _First
    힙의 위치를 첫 번째 요소의 주소를 지정 하는 임의 액세스 반복기입니다.

  • _Last
    마지막 요소는 힙으로 변환할 범위에서 과거의 위치 주소는 임의 액세스 반복기입니다.

  • _Comp
    한 요소가 다른 보다 작은 것이 정의 조건자 함수의 사용자 정의 개체입니다.두 인수를 사용 하 고 반환 하는 이진 술 부 true 만족 스 러 우면 및 거짓 만족 하지 않을 때.

설명

요소는 먼저 다시는 기존 힙 끝까지 밀어넣어야 합니다 및 알고리즘 사용 하 여 기존 힙에이 요소를 추가 하 고.

힙 두 가지 속성이 있습니다.

  • 첫 번째 요소는 항상 가장입니다.

  • 요소 추가 또는 로그 시간에 제거할 수 있습니다.

힙 우선 순위 큐를 구현 하는 이상적인 방법 이며 표준 템플릿 라이브러리 컨테이너 어댑터 구현에 사용 되는 priority_queue 클래스.

참조 범위 유효 해야 합니다. 모든 포인터는 dereferenceable 이어야 하며 마지막 위치에서 첫 번째 접근할 시퀀스 내에서 증분 합니다.

범위 끝에 새로 추가한 요소를 제외한 힙 이어야 합니다.

복잡 한 로그 최대 로그 요구 됩니다 (_Last – _First) 비교 합니다.

예제

// alg_push_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 = 1 ; 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;

   // Add an element to the heap
   v1.push_back( 10 );
   cout << "The heap v1 with 10 pushed back is ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl;

   push_heap( v1.begin( ), v1.end( ) );
   cout << "The reheaped v1 with 10 added is ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl << 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\n ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl;

   v1.push_back(0);
   cout << "The greater-than heap v1 with 11 pushed back is\n ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl;

   push_heap( v1.begin( ), v1.end( ), greater<int>( ) );
   cout << "The greater than reheaped v1 with 11 added is\n ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl;
}

샘플 출력

Vector v1 is ( 9 2 7 3 1 6 8 4 5 ).
The heaped version of vector v1 is ( 9 5 8 4 1 6 7 2 3 ).
The heap v1 with 10 pushed back is ( 9 5 8 4 1 6 7 2 3 10 ).
The reheaped v1 with 10 added is ( 10 9 8 4 5 6 7 2 3 1 ).

The greater-than heaped version of v1 is
 ( 1 2 6 3 5 8 7 4 10 9 ).
The greater-than heap v1 with 11 pushed back is
 ( 1 2 6 3 5 8 7 4 10 9 0 ).
The greater than reheaped v1 with 11 added is
 ( 0 1 6 3 2 8 7 4 10 9 5 ).

요구 사항

헤더: <algorithm>

네임 스페이스: std

참고 항목

참조

heap

표준 템플릿 라이브러리