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


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

C++ 算法 make_heap() 函數用於重新排列範圍 [first, last) 中的元素,使它們形成一個堆。

對於第一個版本使用運算符 < 比較元素,或者對於第二個版本使用給定的二進製比較函數 comp 比較元素。

用法

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

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

參數

first:一個隨機訪問迭代器,指向要轉換成堆的範圍中的第一個元素。

last:一個隨機訪問迭代器,指向要轉換成堆的範圍中過去的最後一個元素。

comp: 一個用戶定義的二元謂詞函數,它接受兩個參數,如果兩個參數按順序返回真,否則返回假。它遵循嚴格的弱排序來對元素進行排序。

返回值

複雜度

複雜性高達第一個和最後一個之間距離的三倍線性:比較元素並可能交換(或移動)它們直到重新排列為更長的堆。

數據競爭

範圍 [first, last) 中的一些對象被修改。

異常

如果任何元素比較、元素交換(或移動)或迭代器上的操作引發異常,則此函數將引發異常。

注意:無效的參數會導致未定義的行為。

例子1

讓我們看一個簡單的例子來演示 make_heap() 的使用:

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

using namespace std;

int main()
{
  vector<int> v = {2, 1, 4, 3};

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

  for_each(v.begin(), v.end(), [](int x) {
    cout << x << endl;
  });
  
  return 0;
}

輸出:

4
3
2
1

例子2

讓我們看另一個簡單的例子:

#include <iostream>     // std::cout
#include <algorithm>    // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap
#include <vector>       // std::vector

using namespace std;

int main () {
  int myints[] = {20,10,30,5,13};
  vector<int> v(myints,myints+5);

  make_heap (v.begin(),v.end());
  cout << "initial max heap  :" << v.front() << '\n';

  pop_heap (v.begin(),v.end()); v.pop_back();
  cout << "max heap after pop:" << v.front() << '\n';

  v.push_back(99); push_heap (v.begin(),v.end());
  cout << "max heap after push:" << v.front() << '\n';

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

  cout << "final sorted range:";
  for (unsigned i=0; i<v.size(); i++)
    cout << ' ' << v[i];

  cout << '\n';

  return 0;
}

輸出:

initial max heap  :30
max heap after pop:20
max heap after push:99
final sorted range:5 10 13 20 99

例子3

讓我們看另一個使用比較函數將任意整數向量轉換為堆的簡單示例:

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
 
using namespace std;
 
int main()
{
  int a[] = {15, 27, 16, 20, 30, 100, 3, 16, 7};
  vector<int> v(a, a+9);

  cout <<"\nHere are the values in the vector:\n";
  for (vector<int>::size_type i=0; i<v.size(); i++)
    cout <<v.at(i)<<" ";
 
  cout <<"\nNow we make these values into a (minimum) heap.";
  make_heap(v.begin(), v.end(), greater<int>());
 
  cout <<"\nHere are the revised contents of the vector:\n";
  for (vector<int>::size_type i=0; i<v.size(); i++)
    cout <<v.at(i)<<" ";
 
  return 0;
}

輸出:

Here are the values in the vector:
15 27 16 20 30 100 3 16 7 
Now we make these values into a (minimum) heap.
Here are the revised contents of the vector:
3 7 15 16 30 100 16 27 20  

示例 4

讓我們看一個簡單的例子:

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

using namespace std;
 
int main()
{
    vector<int> v { 3, 1, 4, 1, 5, 9 };
 
    cout << "initially, v:";
    for (auto i:v) cout << i << ' ';
    cout << '\n';
 
    make_heap(v.begin(), v.end());
 
    cout << "after make_heap, v:";
    for (auto i:v) cout << i << ' ';
    cout << '\n';
 
    pop_heap(v.begin(), v.end());
    auto largest = v.back();
    v.pop_back();
    cout << "largest element:" << largest << '\n';
 
    std::cout << "after removing the largest element, v:";
    for (auto i:v) cout << i << ' ';
    cout << '\n';

return 0;
}

輸出:

initially, v:3 1 4 1 5 9 
after make_heap, v:9 5 4 1 1 3 
largest element:9
after removing the largest element, v:5 3 4 1 1





相關用法


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