用#include <algorithm> 和 #include <vector> 建立一个堆
make_heap 建立堆(传递参数lambada表达式)
压数据进堆 push_heap 弹数据出堆 pop_heap (其实只是放在vector最后一个)
class greater_class{
public:
bool operator()(int a, int b)
{
return a > b;
}
int arr[] = {5,1,6,9,4,3};
vector<int> num(arr,arr+6);
printVector(num);
make_heap(num.begin(), num.end(),[](int a,int b){return a>b;});
printVector(num); // 1 4 3 9 5 6
num.push_back(2);
printVector(num); // 1 4 3 9 5 6 2
push_heap(num.begin(),num.end(),greater_class());
printVector(num); // 1 4 2 9 5 6 3
while (num.size())
{
pop_heap(num.begin(),num.end(),greater_class());
long min = num.back();
num.pop_back();cout << min << std::endl;
} // 1 2 3 4 5 6 9 6 9
附: