STL——heap的4大操作

时间:2022-11-07 15:09:16

STL的堆操作

STL里面的堆操作一般用到的只有4个:make_heap();、pop_heap();、push_heap();、sort_heap();

他们的头文件函数是#include
<algorithm>

首先是make_heap();

函数原型是:void
make_heap(first_pointer,end_pointer,compare_function);

一个参数是数组或向量的头指针,第二个向量是尾指针。第三个参数是比较函数的名字。在缺省的时候,默认是大跟堆。(下面的参数都一样就不解释了)

作用:把这一段的数组或向量做成一个堆的结构。范围是(first,last)

然后是pop_heap();

它的函数原型是:void
pop_heap(first_pointer,end_pointer,compare_function);

作用:pop_heap()不是真的把最大(最小)的元素从堆中弹出来。而是重新排序堆。它
把first和last交换,然后将[first,last-1)的数据再做成一个堆。

接着是push_heap()
void
pushheap(first_pointer,end_pointer,compare_function);

作用:push_heap()假设由[first,last-1)是一个有效的堆,如果不是有效堆就老老实实用make_heap()

然后,再把堆中的新元素加进来,做成一个堆。


最后是sort_heap()void
sort_heap(first_pointer,end_pointer,compare_function);

作用是sort_heap对[first,last)中的序列进行排序。它假设这个序列是有效堆。(当然
,经过排序之后就不是一个有效堆了)

下面是一个数组heap的例子

 #include<algorithm>
#include<cstdio>
using namespace std; bool cmp(int a,int b)//小根堆,降序排序
{
return a>b;
} int main()
{
int i,number[]={,,,,,,,,,,,}; make_heap(&number[],&number[]);
//结果是:51 35 40 23 29 20 26 22 19 12 17 15
for(i=;i<;i++)
{
printf("%d ",number[i]);
}
printf("\n"); make_heap(&number[],&number[],cmp);
//结果:12 17 15 19 23 20 26 51 22 29 35 40
for(i=;i<;i++)
{
printf("%d ",number[i]);
}
printf("\n"); //加入元素8
number[]=;
//加入后调整
push_heap(&number[],&number[],cmp);
//结果:8 17 12 19 29 15 26 22 23 51 35 40 20
for(i=;i<;i++)
{
printf("%d ",number[i]);
}
printf("\n"); //弹出元素8
pop_heap(&number[],&number[],cmp);
//结果:12 17 15 19 23 20 26 51 22 29 35 40 8
for(i=;i<;i++)
{
printf("%d ",number[i]);
}
printf("\n"); sort_heap(&number[],&number[],cmp); //降序
//结果不用说都知道是有序的了!
for(i=;i<;i++)
{
printf("%d ",number[i]);
}
return ;
}

下面是一个vector heap的例子

 #include <iostream>
#include <algorithm>
#include <vector>
using namespace std; const int VECTOR_SIZE = ; int main()
{
vector<int, allocator<int> >Numbers(VECTOR_SIZE) ;
vector<int, allocator<int> >::iterator it ; // Initialize vector Numbers
Numbers[] = ;
Numbers[] = ;
Numbers[] = ;
Numbers[] = ;
Numbers[] = ;
Numbers[] = ;
Numbers[] = ;
Numbers[] = ; // print content of Numbers
cout << "Numbers { " ;
for(it = Numbers.begin(); it != Numbers.end(); it++)
{
cout << *it << " " ;
}
cout << " }\n" << endl ; // convert Numbers into a heap
make_heap(Numbers.begin(), Numbers.end()) ; //缺省参数生成大根堆 cout << "After calling make_heap\n" << endl ;
// print content of Numbers
cout << "Numbers { " ;
for(it = Numbers.begin(); it != Numbers.end(); it++)
{
cout << *it << " " ;
}
cout << " }\n" << endl ; // sort the heapified sequence Numbers
sort_heap(Numbers.begin(), Numbers.end()) ; //缺省参数默认升序排列 cout << "After calling sort_heap\n" << endl ;
// print content of Numbers
cout << "Numbers { " ;
for(it = Numbers.begin(); it != Numbers.end(); it++)
{
cout << *it << " " ;
}
cout << " }\n" << endl ; //insert an element in the heap
Numbers.push_back() ; //在堆的尾部添加一个数据
//push_heap(Numbers.begin(), Numbers.end()) ; //因为之前使用sort对堆进行了排序,所以
//[first,last-1)不是一个有效的堆,push_heap没效果
// you need to call make_heap to re-assert the
// heap property
make_heap(Numbers.begin(), Numbers.end()) ; //使用这个直接全部重新生成新堆 cout << "After calling push_heap and make_heap\n" << endl ; // print content of Numbers
cout << "Numbers { " ;
for(it = Numbers.begin(); it != Numbers.end(); it++)
{
cout << *it << " " ;
}
cout << " }\n" << endl ; // remove the root element from the heap Numbers
pop_heap(Numbers.begin(), Numbers.end()) ; cout << "After calling pop_heap\n" << endl ; // print content of Numbers
cout << "Numbers { " ;
for(it = Numbers.begin(); it != Numbers.end(); it++)
{
cout << *it << " " ;
}
cout << " }\n" << endl ;
return ;
}
/*
程序输出为: Numbers { 4 10 70 10 30 69 96 100 }
之后调用 make_heap Numbers { 100 30 96 10 4 69 70 10 }
之后调用 sort_heap Numbers { 4 10 10 30 69 70 96 100 }
之后调用 push_heap 和 make_heap Numbers { 100 69 96 30 4 70 10 10 7 }
之后调用 pop_heap Numbers { 96 69 70 30 4 7 10 10 100 }
*/