现在的位置: 首页 > 综合 > 正文

min heap

2012年12月17日 ⁄ 综合 ⁄ 共 625字 ⁄ 字号 评论关闭
// range heap example
#include <iostream>
#include
<algorithm>
#include
<vector>
#include
<functional>

using namespace std;

bool cmp( int x, int y)
{
return x > y;
}

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(),cmp);
cout
<< "initial min heap : " << v.front() << endl;

pop_heap (v.begin(),v.end(),cmp);
//pop_heap并没有删除此元素,只是放到对末
cout << "min heap after pop : " << v.front() << endl;

v.push_back(
99); push_heap (v.begin(),v.end(),cmp);
cout
<< "min heap after push: " << v.front() << endl;

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

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

//cout << endl;

return 0;
}

抱歉!评论已关闭.