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


C++ make_heap()用法及代碼示例


make_heap()用於將序列轉換為堆。堆是指向最高(或最低)元素並在O(1)時間內進行訪問的數據結構。所有其他元素的順序取決於特定的實現,但始終保持一致。該函數在標題“算法”中定義。 make_heap()函數有兩種實現。兩者都通過本文進行了解釋。語法1:make_heap(iter_first,iter_last)

Template:
void make_heap (RandomAccessIterator first, RandomAccessIterator last);
參數:
first:The pointer to the starting element of sequence that has to
be transformed into heap.
last:The pointer to the next address to last element of sequence that
has to be transformed into heap.

下麵是演示代碼:


// C++ code to demonstrate the working of  
// make_heap() using syntax 1 
  
#include<iostream> 
#include<algorithm> // for heap  
#include<vector> 
using namespace std; 
  
int main() 
{ 
    // initializing vector; 
    vector<int> vi = { 4, 6, 7, 9, 11, 4 }; 
      
    // using make_heap() to transform vector into 
    // a max heap 
    make_heap(vi.begin(),vi.end()); 
      
    //checking if heap using  
    // front() function 
    cout << "The maximum element of heap is:"; 
    cout << vi.front() << endl; 
      
}

輸出:

The maximum element of heap is:11

語法2:make_heap(iter_first,iter_last,comp)

Template:
void make_heap (RandomAccessIterator first, RandomAccessIterator last, comp);
參數:
first:The pointer to the starting element of sequence that has to be transformed into heap.
last:The pointer to the next address to last element of sequence that has to be transformed into heap.
comp: The comparator function that returns a boolean true/false of the each elements compared. This function accepts two arguments. This can be function pointer or function object and cannot change values.

下麵是演示代碼:

// C++ code to demonstrate the working of  
// make_heap() using syntax 2 
  
#include<iostream> 
#include<algorithm> // for heap  
#include<vector> 
using namespace std; 
  
// comparator function to make min heap 
struct greaters{ 
  bool operator()(const long& a,const long& b) const{ 
    return a>b; 
  } 
}; 
  
int main() 
{ 
    // initializing vector; 
    vector<int> vi = { 15, 6, 7, 9, 11, 45 }; 
      
    // using make_heap() to transform vector into 
    // a min heap 
    make_heap(vi.begin(),vi.end(), greaters()); 
      
    // checking if heap using  
    // front() function 
    cout << "The minimum element of heap is:"; 
    cout << vi.front() << endl; 
      
}

輸出:

The minimum element of heap is:6

可能的應用:此函數可用於計劃。在調度中,將在迭代中動態插入一個新元素。一次又一次地排序以獲得最大的複雜度需要O(nlogn),而不是使用“push_heap()”函數來堆積O(logn)時間中產生的堆。下麵的代碼描述了其實現。

// C++ code to demonstrate   
// application of make_heap() (max_heap) 
// priority scheduling 
  
#include<iostream> 
#include<algorithm> // for heap  
#include<vector> 
using namespace std; 
  
int main() 
{ 
    // initializing vector; 
    // initial job priorities 
    vector<int> vi = { 15, 6, 7, 9, 11, 19}; 
      
    // No. of incoming jobs. 
    int k = 3; 
      
    // using make_heap() to transform vector into 
    // a min heap 
    make_heap(vi.begin(),vi.end()); 
      
    // initializing job variable 
    int a = 10; 
      
    for ( int i=0; i<k; i++) 
    { 
      
    // push a job with priority level 
    vi.push_back(a);  
      
    // transform into heap ( using push_heap() ) 
    push_heap(vi.begin(), vi.end()); 
      
    //checking top priority job 
    // front() function 
    cout << "Job with maximum priority is:"; 
    cout << vi.front() << endl; 
      
    // increasing job priority level 
    a = a + 10; 
      
    } 
      
}

輸出:

ob with maximum priority is:19
Job with maximum priority is:20
Job with maximum priority is:30



相關用法


注:本文由純淨天空篩選整理自 make_heap() in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。