heap c++ *** 作 大顶堆、小顶堆

heap c++  *** 作 大顶堆、小顶堆,第1张

heap c++ *** 作 大顶堆、小顶堆

在C++中,虽然堆不像 vector, set 之类的有已经实现的数据结构,但是在 algorithm.h 中实现了一些相关的模板函数。


下面是一些示例应用

http://www.cplusplus.com/reference/algorithm/pop_heap/

#include <iostream>
#include <algorithm> // make_heap(), pop_heap(), push_heap()
#include <vector>
using namespace std; void printVector(vector<int> &num)
{
for(int i = ; i < num.size(); i++)
cout<<num[i]<<" ";
cout<<endl;
}
int main()
{
// init
int arr[] = {,,,,,};
vector<int> num(arr,arr+);
printVector(num); // build
make_heap(num.begin(),num.end());
printVector(num); // 9 5 6 1 4 3 默认大顶堆 // get the biggest number
// 从vector的角度来取得
cout<<num[]<<endl; // 9
// or num.front();
cout<<num.front()<<endl; // 9 // delete 堆顶,即最大的元素
// 返回值为 void
// 将堆顶的元素放到最后一个位置上
// d出一个元素后,剩下的又重建了 heap,仍保持heap的性质
pop_heap(num.begin(),num.end());
printVector(num); // 6 5 3 1 4 9
// vector 删除末尾元素
num.pop_back();
printVector(num); num.push_back(); //首先在vector上扩容,增加一个元素到尾部
printVector(num); // 6 5 3 1 4 7
push_heap(num.begin(),num.end()); // 指定区间的最后一个元素加入堆中并使整个区间成为一个新的堆。


注意前提是最后一个元素除外的所有元素已经构成一个堆。



printVector(num); // 7 5 6 1 4 3 // 判断是否为堆
bool ret = is_heap(num.begin(),num.end());
cout<<ret<<endl;
num.push_back();
printVector(num); // 7 5 6 1 4 3 9
cout<< is_heap(num.begin(),num.end()) <<endl;
push_heap(num.begin(),num.end());
printVector(num); // 9 5 7 1 4 3 6 sort_heap(num.begin(),num.end());
printVector(num); // 1 3 4 5 6 7 9
} // 小顶堆
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; class greater_class{
public:
bool operator()(int a, int b)
{
return a > b;
}
}; int main()
{
// init
int arr[] = {,,,,,};
vector<int> num(arr,arr+);
printVector(num); make_heap(num.begin(), num.end(), greater_class());
printVector(num); // 1 4 3 9 5 6 num.push_back();
printVector(num); // 1 4 3 9 5 6 2
push_heap(num.begin(),num.end(),greater_class());
printVector(num); // 1 4 2 9 5 6 3 while (num.size())
{
pop_heap(num.begin(),num.end(),greater_class());
long min = num.back();
num.pop_back();
cout << min << std::endl;
} // 1 2 3 4 5 6 9
}

  

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/586437.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-12
下一篇 2022-04-12

发表评论

登录后才能评论

评论列表(0条)

保存