priority_queue的实现
代码
#pragma once
#include <vector>
#include <iostream>
using namespace std;
namespace Freedany {
template<class T>
struct less
{
bool operator()(const T& x,const T& y) {
return x < y;
}
};
template <class T>
struct greater
{
bool operator()(const T& x, const T& y) {
return x > y;
}
};
template <class T,class Container = vector<T>,class Compare = less<T>>
class priority_queue {
public:
void adjust_up(int child) {
Compare com;
int parent = (child - 1) / 2;
while (child>0) {
if (com(_con[child] , _con[parent])) {
swap(_con[child], _con[parent]);
child = parent;
parent = (child - 1) / 2;
}
else {
break;
}
}
}
void adjust_down(size_t parent) {
Compare com;
size_t child = parent * 2 + 1 ;
while (child<_con.size())
{
if (child + 1 < _con.size() && com(_con[child] , _con[child+1]))
++child;
if (com(_con[parent] , _con[child])) {
swap(_con[child], _con[parent]);
parent = child;
child = parent * 2 + 1;
}
else {
break;
}
}
}
void push(const T& x) {
_con.push_back(x);
adjust_up(_con.size()-1);
}
void pop() {
swap(_con[0], _con[_con.size() - 1]);
_con.pop_back();
adjust_down(0);
}
const T& top() {
return _con[0];
}
size_t size() {
return _con.size();
}
bool empty() {
return _con.empty();
}
private:
Container _con;
};
void test_priority_queue()
{
//priority_queue<int, vector<int>, greater<int>> pq;
priority_queue<int> pq;
//priority_queue<int, deque<int>> pq;
pq.push(1);
pq.push(0);
pq.push(5);
pq.push(2);
pq.push(1);
pq.push(7);
while (!pq.empty())
{
cout << pq.top() << " ";
pq.pop();
}
cout << endl;
}
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
: _year(year)
, _month(month)
, _day(day)
{}
bool operator<(const Date& d)const
{
return (_year < d._year) ||
(_year == d._year && _month < d._month) ||
(_year == d._year && _month == d._month && _day < d._day);
}
bool operator>(const Date& d)const
{
return (_year > d._year) ||
(_year == d._year && _month > d._month) ||
(_year == d._year && _month == d._month && _day > d._day);
}
friend ostream& operator<<(ostream& _cout, const Date& d)
{
_cout << d._year << "-" << d._month << "-" << d._day;
return _cout;
}
private:
int _year;
int _month;
int _day;
};
class PDateLess
{
public:
bool operator()(const Date* p1, const Date* p2)
{
return *p1 < *p2;
}
};
class PDateGreater
{
public:
bool operator()(const Date* p1, const Date* p2)
{
return *p1 > *p2;
}
};
void test_priority_queue2()
{
// 大堆,需要用户在自定义类型中提供<的重载
//priority_queue<Date> q1;
priority_queue<Date, vector<Date>, greater<Date>> q1;
q1.push(Date(2018, 10, 29));
q1.push(Date(2018, 10, 30));
q1.push(Date(2018, 10, 28));
cout << q1.top() << endl;
//priority_queue<Date*, vector<Date*>, PDateLess> q2;
priority_queue<Date*, vector<Date*>, PDateGreater> q2;
q2.push(new Date(2018, 10, 29));
q2.push(new Date(2018, 10, 30));
q2.push(new Date(2018, 10, 28));
cout << *(q2.top()) << endl;
}
}
struct和class的区别,什么时候用哪一个?
在定义类的时候 用class和struct都是可以的 这两个就是默认的访问权限不一样 class的默认访问权限是private struct的默认访问权限是public
不过定义类在C++中 一般优先使用class 在定义类似于二叉树节点这种结构的时候 用struct居多
另外在模板参数列表中 template 中只能用class 或者 typename 不能用struct
class Compare的作用
有了这个参数以后,可以自己设计Compare的比较方式
对priority_queue的理解
priority_queue是C++中内置建堆的方法,它继承了堆的特性和优点,能够在大量数据中快速找出最大或者最小的k个数。在template的三个参数中