共用方式為


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

標準樣板程式庫