pop_heap(_RAIter, _RAIter, _Compare):最近在分析别人代码的时候,不懂pop_heap, pop_back, push_heap:在此记录一下自己的学习所得,欢迎各位大神点评:
#include <vector>
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
class Index
{
public:
Index(int a, float b)
{
i = a;
cost = b;
}
int i;
float cost;
};
struct greater1
{
bool operator()(const Index& a, const Index& b) const // operator() 调用操作符()
{ // 当调用操作符的时候,相当于调用函数,(这处没有函数名)
printf("操作符 a cost = %lf ,b cost = %lf\n", a.cost, b.cost);
return a.cost > b.cost;// a>b是大最小堆 ::a<b 是最大堆
}
};
void print(vector<Index> sum);
void print1(vector<int> sum);
int main(int argc, char *argv[])
{
vector<Index> a1;
Index index(0,0);
// for(int i=0; i<10; i++)
// {
// index.cost = (i)*100+1;
// index.i = i;
// a1.push_back(index);
// }
index.i = 0; index.cost = 1;
a1.push_back(index);
index.i = 1; index.cost = 101;
a1.push_back(index);
index.i = 2; index.cost = 201;
a1.push_back(index);
print(a1);
pop_heap(a1.begin(), a1.end(), greater1());//当函数中第一个值小于最后一个值就行交换
printf("进行pop heap 之后 ");
print(a1);
a1.pop_back();//删除最后一个值
printf("进行pop back 之后 将最后一个值删除");
print(a1);
index.i = 11;
index.cost = 2;//插入一个新值
a1.push_back(index);
printf("插入一个新值之后");
print(a1);
push_heap(a1.begin(), a1.end(), greater1());
//push_heap 会根据所构造是什么堆,来将新插入的值放到什么位置
printf("进行push heap之后 将最后一个值删除");
print(a1);
return 0;
}
引用:感谢下面博主
http://www.cplusplus.com/reference/algorithm/push_heap/
https://blog.csdn.net/l_tudou/article/details/51504477
https://www.1024do.com/?p=3378