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

using namespace std;

void PrinfVectorInt(vector<int> &vec)
{
for(auto iter = vec.begin(); iter != vec.end(); ++iter)
{
cout << *iter << ", ";
}
cout << endl;
}

bool Greater(int & data_left, int & data_right)
{
return data_left >= data_right;
}

int main ()
{
vector<int> vecMinHeap = {11, 54, 32, 45, 65, 76, 89, 4, 45, 13,25, 2, };

//建堆
make_heap(vecMinHeap.begin(), vecMinHeap.end(), Greater);
PrinfVectorInt(vecMinHeap);
if(30 > vecMinHeap.front())
{
pop_heap(vecMinHeap.begin(), vecMinHeap.end(), Greater);
vecMinHeap.back() = 30;
push_heap(vecMinHeap.begin(), vecMinHeap.end(), Greater);
}

PrinfVectorInt(vecMinHeap);
if(4 > vecMinHeap.front())
{
pop_heap(vecMinHeap.begin(), vecMinHeap.end(), Greater);
vecMinHeap.back() = 4;
push_heap(vecMinHeap.begin(), vecMinHeap.end(), Greater);
}

PrinfVectorInt(vecMinHeap);

if(132 > vecMinHeap.front())
{

pop_heap(vecMinHeap.begin(), vecMinHeap.end(), Greater);
vecMinHeap.back() = 132;
push_heap(vecMinHeap.begin(), vecMinHeap.end(), Greater);
}
PrinfVectorInt(vecMinHeap);
return 1;
}

//使用了c++11 的特性,所以需要使用c++11的库编译

g++ --std=c++11 -o min_head min_heap.cpp

posted on 2018-05-08 16:23  c碰  阅读(4092)  评论(0编辑  收藏  举报