当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ Algorithm pop_heap()用法及代码示例


C++ 算法 pop_heap() 函数用于交换位置 ?first? 中的值。以及位置 ?last-1? 中的值?并使子范围 [first, last-1) 成为最大堆。它具有从范围 [first, last) 定义的堆中删除第一个(最大)元素的效果。

对于第一个版本使用运算符 < 比较元素,或者对于第二个版本使用给定的二进制比较函数 comp 比较元素。

注意:最大堆是具有以下属性的元素范围 [first, last):

  • N = last - first,对于所有 0 < i < N,f [floor (i-1, 2)] 不小于 f[i]。
  • 可以使用 std::push_heap() 添加新元素
  • 可以使用 std::pop_heap() 删除第一个元素

用法

default (1)     template <class RandomAccessIterator>
                         void pop_heap (RandomAccessIterator first, RandomAccessIterator last);

custom (2)    template <class RandomAccessIterator, class Compare>
                       void pop_heap (RandomAccessIterator first, RandomAccessIterator last,
                                                Compare comp);

参数

first:指向堆中第一个元素的随机访问迭代器。

last:指向堆中过去最后一个元素的随机访问迭代器。

comp: 一个用户定义的二元谓词函数,它接受两个参数,如果两个参数按顺序返回真,否则返回假。它遵循严格的弱排序来对元素进行排序。

返回值

复杂度

复杂性在第一个和最后一个之间的距离上是对数的两倍:比较元素并可能交换(或移动)它们直到重新排列为更长的堆。

数据竞争

范围 [first, last) 中的一些对象被修改。

异常

如果元素比较、元素交换或迭代器上的操作中的任何一个抛出异常,则此函数将抛出异常。

注意:无效的参数会导致未定义的行为。

例子1

让我们看一个简单的例子来演示 pop_heap() 的使用:

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main(){
   const int SIZE = 10;
   int a[ SIZE ] = { 13, 10, 152, 77, 22, 31, 1, 98, 13, 40 };
   int j;
   vector< int > v
   ( a, a + SIZE ), v2;

   make_heap( v.begin(), v.end() );
   sort_heap( v.begin(), v.end() );

   for ( j=0; j < SIZE; j++ ) 
{
      v2.push_back( a[ j ] );
      push_heap( v2.begin(), v2.end() );      
   }
   
   for ( j = 0; j < v2.size(); j++ ) 
{
      cout << "\nv2 after " << v2[ 0 ] << " popped from heap\n";
      pop_heap( v2.begin(), v2.end() - j );
   }

   cout << endl;
   return 0;
}

输出:

v2 after 152 popped from heap

v2 after 98 popped from heap

v2 after 77 popped from heap

v2 after 40 popped from heap

v2 after 31 popped from heap

v2 after 22 popped from heap

v2 after 13 popped from heap

v2 after 13 popped from heap

v2 after 10 popped from heap

v2 after 1 popped from heap

例子2

让我们看另一个简单的例子:

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;
 
int main()
{
    vector<int> v { 3, 1, 4, 1, 5, 9 };
 
    make_heap(v.begin(), v.end());
 
    cout << "v:";
    for (auto i:v) cout << i << ' ';
    cout << '\n';
 
    pop_heap(v.begin(), v.end()); // moves the largest to the end
 
    cout << "after pop_heap:";
    for (auto i:v) cout << i << ' ';
    cout << '\n';
 
    int largest = v.back();
    v.pop_back();  // actually removes the largest element
    cout << "largest element:" << largest << '\n';
 
    cout << "heap without largest:";
    for (auto i:v) cout << i << ' ';
    cout << '\n';
    
    return 0;
}

输出:

v:9 5 4 1 1 3 
after pop_heap:5 3 4 1 1 9 
largest element:9
heap without largest:5 3 4 1 1

例子3

让我们看另一个简单的例子:

#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <iterator>
#include <functional>
#include <numeric>

/* PRINT_ELEMENTS()
 * - prints optional C-string optcstr followed by
 * - all elements of the collection coll
 * - separated by spaces
 */
template <class T>
inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    std::cout << optcstr;
    for (pos=coll.begin(); pos!=coll.end(); ++pos) {
        std::cout << *pos << ' ';
    }
    std::cout << std::endl;
}

/* INSERT_ELEMENTS (collection, first, last)
 * - fill values from first to last into the collection
 * - NOTE:NO half-open range
 */
template <class T>
inline void INSERT_ELEMENTS (T& coll, int first, int last)
{
    for (int i=first; i<=last; ++i) {
        coll.insert(coll.end(),i);
    }
}


using namespace std;

int main()
{
    vector<int> coll;

    INSERT_ELEMENTS(coll,3,7);
    INSERT_ELEMENTS(coll,5,9);
    INSERT_ELEMENTS(coll,1,4);

    PRINT_ELEMENTS (coll, "on entry:          ");

    // convert collection into a heap
    make_heap (coll.begin(), coll.end());

    PRINT_ELEMENTS (coll, "after make_heap(): ");

    // pop next element out of the heap
    pop_heap (coll.begin(), coll.end());
    coll.pop_back();

    PRINT_ELEMENTS (coll, "after pop_heap():  ");

    return 0;    
}

输出:

on entry:          3 4 5 6 7 5 6 7 8 9 1 2 3 4 
after make_heap(): 9 8 6 7 7 5 5 3 6 4 1 2 3 4 
after pop_heap():  8 7 6 7 4 5 5 3 6 4 1 2 3 

示例 4

让我们看一个简单的例子:

#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;  
   
   return 0;
}

输出:

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

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





相关用法


注:本文由纯净天空筛选整理自 C++ Algorithm pop_heap()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。