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
    处理第一元素位置的随机访问迭代器。堆。

  • _Last
    寻址最终元素的随机访问迭代器位置一堆中。

  • _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 ).

要求

标头: <算法>

命名空间: std

请参见

参考

heap

标准模板库