STL堆之三

//如果一开始用greater,则之后调用的push_heap,pop_heap,sort_heap都要带greater,否则出现异常:invalid heap

#include
<iostream>
#include
<algorithm>
#include
<vector>
#include
<functional>
using namespace std;

void print_ivec(vector<int>::iterator begin, vector<int>::iterator end)
{
for(;begin != end; ++begin)
cout
<< *begin << " ";
cout
<< endl;
}
int main()
{
int a[] = {1, 188, 15, 20, 30};
vector
<int> ivec(a, a + sizeof(a) / sizeof(a[0]));
print_ivec(ivec.begin(), ivec.end());

make_heap(ivec.begin(), ivec.end(), greater
<int>());
print_ivec(ivec.begin(), ivec.end());

pop_heap(ivec.begin(), ivec.end(),greater
<int>());
ivec.pop_back();
print_ivec(ivec.begin(), ivec.end());

ivec.push_back(
99);
push_heap(ivec.begin(), ivec.end(),greater
<int>());
print_ivec(ivec.begin(), ivec.end());

sort_heap(ivec.begin(), ivec.end(),greater
<int>());
print_ivec(ivec.begin(), ivec.end());

return 0;
}

  

posted on 2011-08-24 16:14  sysu_mjc  阅读(142)  评论(0编辑  收藏  举报

导航