关于STL中heap的用法

来源:互联网 发布:淘宝韩国代购真假 编辑:程序博客网 时间:2024/05/19 15:24

在STL中heap的用法主要是make_heap(),push_heap(),pop_heap()的用法。具体均在代码中:

// range heap example  用heap构造出来的实际上是一棵树

#include <iostream>#include <algorithm>#include <vector>using namespace std;int main (){int myints[] = {10,20,30,5,15};vector<int> v(myints,myints+5);vector<int>::iterator it;make_heap (v.begin(),v.end());//male_heap就是构造一棵树,使得每个父结点均大于等于其子女结点cout << "initial max heap   : " << v.front() << endl;/* */pop_heap (v.begin(),v.end());//pop_heap不是删除某个元素而是把第一个和最后一个元素对调后[first,end-1]进行构树,最后一个不进行构树v.pop_back();//删除最后一个的结点cout << "max heap after pop : " << v.front() << endl;v.push_back(99);//在最后增加一个结点push_heap (v.begin(),v.end());//重新构树cout << "max heap after push: " << v.front() << endl;sort_heap (v.begin(),v.end());//把树的结点的权值进行排序  /*for(int j=0;j<v.size();j++)  cout<<v[j]<<' ';cout<<endl;*/cout << "final sorted range :";for (unsigned i=0; i<v.size(); i++) cout << " " << v[i];cout << endl;return 0;}

原创粉丝点击