共用方式為


pop_heap

從堆積的前面移除最大的項目對應至範圍的倒數第二個位置然後形成從其餘項目的新的堆積。

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

參數

  • _First
    解決隨機存取 Iterator 的第一個項目位置在堆積。

  • _Last
    解決隨機存取 Iterator 到最後的項目位置的堆積。

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

備註

pop_heap 演算法是 push_heap 演算法執行之作業的回應,項目在範圍倒數第二個位置加入到包含在這個範圍的堆積之前的項目,在這種情況下,則在加入堆積的項目大於任何項目已經在堆積上。

堆積具有兩個屬性:

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

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

堆積是理想的方式實作優先權佇列,以及在標準樣板程式庫 (STL) 容器配接器 priority_queue 類別的實作。

參考的範圍必須是有效的;所有指標必須 dereferenceable,並在序列中最後一個位置從開始取用的增量。

除了新增項目的範圍在結尾必須是堆積。

複雜是對數的要求,而且記錄 (_Last – _First) 比較。

範例

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

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

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

   // Make v1 a heap with default less than ordering
   random_shuffle( v1.begin( ), v1.end( ) );
   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 back of the heap
   v1.push_back( 10 );
   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;

   // Remove the largest element from the heap
   pop_heap( v1.begin( ), v1.end( ) );
   cout << "The heap v1 with 10 removed is ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl << endl;

   // Make v1 a heap with greater-than ordering with a 0 element
   make_heap ( v1.begin( ), v1.end( ), greater<int>( ) );
   v1.push_back( 0 );
   push_heap( v1.begin( ), v1.end( ), greater<int>( ) );
   cout << "The 'greater than' reheaped v1 puts the smallest "
        << "element first:\n ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl;

   // Application of pop_heap to remove the smallest element
   pop_heap( v1.begin( ), v1.end( ), greater<int>( ) );
   cout << "The 'greater than' heaped v1 with the smallest element\n "
        << "removed from the heap is: ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl;
}

範例輸出

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

The 'greater than' reheaped v1 puts the smallest element first:
 ( 0 1 6 3 2 8 7 4 9 10 5 ).
The 'greater than' heaped v1 with the smallest element
 removed from the heap is: ( 1 2 6 3 5 8 7 4 9 10 0 ).

需求

標頭:<algorithm>

命名空間: std

請參閱

參考

heap

標準樣板程式庫