⭐博客主页:️CS semi主页
⭐欢迎关注:点赞收藏+留言
⭐系列专栏:C++初阶
⭐代码仓库:C++初阶
家人们更新不易,你们的点赞和关注对我而言十分重要,友友们麻烦多多点赞+关注,你们的支持是我创作最大的动力,欢迎友友们私信提问,家人们不要忘记点赞收藏+关注哦!!!
优先级队列放在这里进行单独讲解,是不同于前面的队列和栈的,它所运用到的数据结构中的知识很多,需要用到堆的知识。
默认类型:
#include
#include
#include
using namespace std;
int main()
{
// 默认是大堆 也就是降序
//priority_queue pq;
// 仿函数控制实现小堆 也就是升序
priority_queue<int, vector<int>, greater<int>> pq;
pq.push(4);
pq.push(1);
pq.push(3);
pq.push(5);
pq.push(2);
while (!pq.empty())
{
cout << pq.top() << " ";
pq.pop();
}
cout << endl;
return 0;
}
自定义类型:
#include
#include
#include
using namespace std;
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;
};
void TestPriorityQueue()
{
// 大堆,需要用户在自定义类型中提供<的重载
priority_queue<Date> q1;
q1.push(Date(2018, 10, 29));
q1.push(Date(2018, 10, 28));
q1.push(Date(2018, 10, 30));
cout << q1.top() << endl;
// 如果要创建小堆,需要用户提供>的重载
priority_queue<Date, vector<Date>, greater<Date>> q2;
q2.push(Date(2018, 10, 29));
q2.push(Date(2018, 10, 28));
q2.push(Date(2018, 10, 30));
cout << q2.top() << endl;
}
int main()
{
TestPriorityQueue();
return 0;
}
方法一:先利用优先级队列将其建成大堆,然后再利用循环将前k-1个元素都pop出去,再输出顶部的数据就为题解。
方法二:建成小堆,将k个元素先放到堆里面去,再遍历后面的数,比顶元素大的值替换它,直到最后一个,最终栈顶的元素就是k大的数。这样的时间复杂度很低。
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
priority_queue<int> pq(nums.begin(), nums.end());
while(--k)
{
pq.pop();
}
return pq.top();
}
};
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
// 建小堆
priority_queue<int, vector<int>, greater<int>> pq(nums.begin(), nums.begin() + k);
for(int i = k; i < nums.size(); ++i)
{
if(nums[i] > pq.top())
{
pq.pop();
pq.push(nums[i]);
}
}
// 找到第二大的数
return pq.top();
}
};
priority_queue.h:
#pragma once
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 JRH
{
template<class T, class container = vector<int>, class Compare = Less<T>>
class priority_queue
{
private:
void AdjustDown(int parent)
{
// 利用仿函数以及库里面的less函数
Compare com;
// 找左右孩子大的那个
int 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 AdjustUp(int child)
{
Compare 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_queue()
{
// 默认是大堆
priority_queue<int, vector<int>, Greater<int>> pq;
pq.push(4);
pq.push(1);
pq.push(3);
pq.push(5);
pq.push(2);
while (!pq.empty())
{
cout << pq.top() << " ";
pq.pop();
}
cout << endl;
}
}
test.cpp:
#include
#include
#include
using namespace std;
#include"priority_queue.h"
int main()
{
JRH::test_priority_queue();
return 0;
}
这样子其实仿函数更加好理解了,我们利用一个自己写的或者是库里面写的都可以,利用仿函数,我们的>和<就没那么刻板了,我们利用仿函数,让它们比较进入到我们规定的区域内进行比较即可。