共用方式為


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
    解決隨機存取 Iterator 的第一個項目位置在堆積。

  • _Last
    解決隨機存取 Iterator 將最後一個項目的位置會在範圍切換至堆積。

  • _Comp
    使用者定義的述詞函式物件,定義在此意義上某個項目小於另一個。 符合時,二進位述詞會採用兩個引數並傳回 true ,不符合時則傳回 false

備註

必須先推入項目至現有堆積的結尾演算法可用來將項目加入至現有的堆積。

堆積具有兩個屬性:

  • 第一個項目永遠是最大。

  • 項目在對的時間可能會加入或移除。

堆積是理想的方式實作優先權佇列,以及在標準樣板程式庫 (STL) 容器配接器 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

標準樣板程式庫