-
STL中没有把heap作为一种容器组件,heap的实现需要更低一层的容器组件(诸如list,array,vector)作为其底层机制。Heap是一个类属算法,包含在algorithm头文件中。
-
STL中关于heap默认调整成的是大顶堆,但却可以让用户利用自定义的compare_fuction函数实现大顶堆或小顶堆。heap的低层机制vector本身就是一个类模板,heap基于vector便实现了对各种数据类型(无论基本数据类型还是用户自定义的数据类型)的堆排(前提是用户自定义的数据类型要提供比较机制compare_fuction函数)。
-
STL中与堆相关的4个函数:
参数:一个参数是数组或向量的头指针,第二个向量是尾指针。第三个参数是比较函数的名字。在缺省的时候,默认是大根堆。
建立堆make_heap():
void make_heap(first_pointer,end_pointer,compare_function)
作用:把这一段的数组或向量做成一个堆的结构。范围是(first,last)。
在堆中添加数据push_heap():
void pushheap(first_pointer,end_pointer,compare_function);
作用:push_heap()假设由[first,last-1)是一个有效的堆,然后,再把堆中的新元素加进来,做成一个堆。
在堆中删除数据pop_heap():
void pop_heap(first_pointer,end_pointer,compare_function);
作用:pop_heap()不是真的把最大(最小)的元素从堆中弹出来。而是重新排序堆。它把first和last交换,然后将[first,last-1)的数据再做成一个堆。
堆排序sort_heap():
void sort_heap(first_pointer,end_pointer,compare_function);
作用是sort_heap对[first,last)中的序列进行排序。它假设这个序列是有效堆。(当然,经过排序之后就不是一个有效堆了) -
头文件
#include <algorithm>
-
第三个参数:
1)采用仿函数对象 ,对应的头文件为:#include<functional>
。
小顶堆–>greater()
大顶堆—>less()
2)采用自定义函数名,并将该函数名传递给第三个参数,具体如下:
bool less_cmp(const int &a,const int &b)//等价于: less<int>()
{
return a<b;
}
bool greater_cmp(const int &a, const int &b) //等价于: greater<int>()
{
return a>b;
}
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int a[9] = { 0,1,6,5,4,9,8,7,5 };
make_heap(a, a + 9, less<int>());
sort_heap(a, a + 9, less<int>());
cout << "-------------1------------------" << endl;
for (int i = 0; i < 9; i++)
{
cout << a[i];
}
cout << endl;
cout << "------------------------------------" << endl;
make_heap(a, a + 9, less<int>());
pop_heap(a, a + 9, less<int>());
cout << a[8] << endl;
vector<int> vec(a, a + 9);
make_heap(vec.begin(), vec.end(), less<int>());
cout << "-------------2-----------------" << endl;
for (unsigned int i = 0; i < vec.size(); i++)
{
cout << vec[i];
}
cout << endl;
cout << "------------------------------" << endl;
vec.push_back(7);
push_heap(vec.begin(), vec.end(), less<int>());
cout << "-------------3------------------" << endl;
for (unsigned int i = 0; i < vec.size(); i++)
{
cout << vec[i];
}
cout << endl;
cout << "-------------------------------" << endl;
pop_heap(vec.begin(), vec.end(), less<int>());
cout << vec.back() << endl;
vec.pop_back();
cout << "-------------4------------------" << endl;
for (unsigned int i = 0; i < vec.size(); i++)
{
cout << vec[i];
}
cout << endl;
cout << "-------------------------------" << endl;
sort_heap(vec.begin(), vec.end(), less<int>());
cout << "-------------5------------------" << endl;
for (unsigned int i = 0; i < vec.size(); i++)
{
cout << vec[i];
}
cout << endl;
cout << "-------------------------------" << endl;
system("pause");
return 0;
}
#include <iostream>
#include <algorithm>
using namespace std;
bool cmp(int a, int b)
{
return a > b;
}
int main()
{
int i;
int num[20] = { 29,23,20,22,17,15,26,51,19,12,35,40 };
make_heap(num, num + 12);
cout << "默认是大顶堆" << endl;
for (i = 0; i < 12; i++)
{
cout << num[i] << " ";
}
cout << endl;
make_heap(num, num + 12, cmp);
cout << "实现小顶堆" << endl;
for (i = 0; i < 12; i++)
{
cout << num[i] << " ";
}
cout << endl;
make_heap(num, num + 12, less<int>());
cout << "实现大顶堆" << endl;
for (i = 0; i < 12; i++)
{
cout << num[i] << " ";
}
cout << endl;
num[12] = 8;
push_heap(num, num + 13, cmp);
cout << "-----------------" << endl;
for (i = 0; i < 13; i++)
{
cout << num[i] << " ";
}
cout << endl;
pop_heap(num, num + 13, cmp);
cout << "-----------------" << endl;
for (i = 0; i < 12; i++)
{
cout << num[i] << " ";
}
cout << endl;
system("pause");
return 0;
}