# c++: c++ make_heap, pop_heap, push_heap, sort_heap详解

std::pop_heap是一个实现快排的库

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
int a[] = {100, 19, 36, 17, 3, 25, 1, 2, 7};
vector<int> v(a, a+9);

cout <<"\nHere are the values in the vector (the heap):\n";
for (vector<int>::size_type i=0; i<v.size(); i++)
cout <<v.at(i)<<" ";

cout <<"\nNow we delete (pop) a value from the heap.";
pop_heap(v.begin(), v.end());

cout <<"\nHere are the revised contents of the vector:\n";
for (vector<int>::size_type i=0; i<v.size(); i++)
cout <<v.at(i)<<" ";
cout <<"\nNote that the value deleted from the heap is still "
"\nin the vector (at the end of the vector).";

cout <<"\nSo, we should reduce the size of the vector by 1, which "
"we now do, and\nthen display the vector, which is once again a heap, one more time.";

v.resize(v.size()-1);
cout <<"\nHere are the final contents of the vector:\n";
for (vector<int>::size_type i=0; i<v.size(); i++)
cout <<v.at(i)<<" ";

return 0;
}

Output:

Here are the values in the vector (the heap):
100 19 36 17 3 25 1 2 7

Now we delete (pop) a value from the heap.

Here are the revised contents of the vector:
36 19 25 17 3 7 1 2 100
Note that the value deleted from the heap is still
in the vector (at the end of the vector).

So, we should reduce the size of the vector by 1, which we now do, and
then display the vector, which is once again a heap, one more time.

Here are the final contents of the vector:
36 19 25 17 3 7 1 2

## 堆的主要相关算法介绍

ＰＳ:只对比一次

### sort_heap算法

ＰＳ:　sort_heap时，必须先是一个堆（两个特性：1、最大元素在第一个 2、添加或者删除元素以对数时间），因此必须先做一次make_heap.