當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。