函数 | 作用 |
---|---|
make_heap | 构建大顶锥 |
make_heap(v.begin(), v.end(), greater<int>()); | 构建小顶锥 |
pop_heap | 将堆顶元素移动到last-1位置上 |
push_heap | 在加入新元素后,重建堆 |
sort_heap | 排序(从小到大) |
示例代码
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int a[] = {10,20,30,5,15};
vector<int> v(a,a+5);
vector<int>::iterator it;
//make_heap
make_heap(v.begin(),v.end());
cout << "make_heap" << endl;
for(it=v.begin();it!=v.end();it++){
cout << *it << ' ';
}
cout << endl;
//pop_heap
cout << "pop_heap" << endl;
pop_heap(v.begin(),v.end());
for(it=v.begin();it!=v.end();it++){
cout << *it << ' ';
}
cout << endl;
//sort_heap
cout << "sort_heap" << endl;
sort_heap (v.begin(),v.end());
for(it=v.begin();it!=v.end();it++){
cout << *it << ' ';
}
cout << endl;
make_heap(v.begin(),v.end());
//push_heap
cout << "re_make_heap" << endl;
for(it=v.begin();it!=v.end();it++){
cout << *it << ' ';
}
cout << endl;
v.push_back(99);
push_heap (v.begin(),v.end());
cout << "push_heap" << endl;
for(it=v.begin();it!=v.end();it++){
cout << *it << ' ';
}
cout << endl;
return 0;
}
结果
网友评论