优先队列和队列是在同一个头文件中 也就是#include<queue>
优先队列底层就是二叉树的堆
默认大的优先级高(大堆)
和栈和队列一样 不支持遍历和[]
如果要小堆的话那就要改第三个参数了
priority_queue<int,vector<int>,greater<int>> pq
greater改成less就是大堆了(greater和less本质上都是仿函数)
我们思考一下 这个地方greater<int>后面要不要加括号?
答案是不用
这个地方是类模板要传的是参数 参数是什么? 是类型 所以就不可以加括号
但是什么时候要用比如像这样sort(pq.begin(),pq.end(),greater<int>())
这个时候就需要 为什么?
因为这个地方是函数模板
函数模板要传对象 所以这里本质上是一个匿名对象greater<int>()
下面是优先队列实现的代码
#pragma once
#include<vector>
#include<iostream>
using namespace std;
// 仿函数/函数对象
template<class T>
class Less
{
public:
bool operator()(const T& x, const T& y)
{
return x < y;
}
};
template<class T>
class Greater
{
public:
bool operator()(const T& x, const T& y)
{
return x > y;
}
};
namespace bit
{
template<class T, class Container = vector<T>, class Comapre = less<T>>
class priority_queue
{
private:
void AdjustDown(int parent)
{
Comapre com;
// 找左右孩子大的那一个
size_t child = parent * 2 + 1;
while (child < _con.size())
{
if (child + 1 < _con.size() && com(_con[child], _con[child + 1]))
//if (child + 1 < _con.size() && _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 AdjustUp(int child)
{
Comapre com;
int parent = (child - 1) / 2;
while (child > 0)
{
if (com(_con[parent], _con[child]))
{
swap(_con[child], _con[parent]);
child = parent;
parent = (child - 1) / 2;
}
else
{
break;
}
}
}
public:
priority_queue()
{
}
template<class InputIterator>
priority_queue(InputIterator first, InputIterator last)
{
while (first != last)
{
_con.push_back(*first);
++first;
}
// 建堆
for (int i = (_con.size() - 1 - 1) / 2; i >= 0; i--)
{
AdjustDown(i);
}
}
void pop()
{
swap(_con[0], _con[_con.size() - 1]);
_con.pop_back();
AdjustDown(0);
}
void push(const T& x)
{
_con.push_back(x);
AdjustUp(_con.size() - 1);
}
const T& top()
{
return _con[0];
}
bool empty()
{
return _con.empty();
}
size_t size()
{
return _con.size();
}
private:
Container _con;
};
void test_priority_queue1()
{
// 默认是大堆 -- less
//priority_queue<int> pq;
// 仿函数控制实现小堆
priority_queue<int, vector<int>, Greater<int>> pq;
pq.push(3);
pq.push(5);
pq.push(1);
pq.push(4);
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);
private:
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream& _cout, const Date& d)
{
_cout << d._year << "-" << d._month << "-" << d._day;
return _cout;
}
struct LessPDate
{
bool operator()(const Date* p1, const Date* p2)
{
return *p1 < *p2;
}
};