一、优先队列的介绍
priority_queue
(优先队列)是 C++ 标准模板库(STL)中的一个容器适配器。它的底层实现通常是用堆(heap)这种数据结构。在priority_queue
中默认是大堆。这意味着元素按照一定的优先级顺序排列,优先级最高的元素总是在队首,就像排队时享有优先权的人排在前面一样。
二、优先队列的基本操作
priority_queue支持以下操作:
empty():检测容器是否为空
size():返回容器中有效元素个数
top():返回优先级队列中最大(最小)元素,即堆顶元素
push_back():在容器尾部插入元素pop_back():删除容器尾部元素
要使用priority_queue
,需要包含<queue>
头文件。
#include <queue>
using namespace std;
#include<iostream>
#include<queue>
#include"My_priority_queue.h"
void test_priority_queue1()
{
// 默认大的优先级高,底层是个大堆
priority_queue<int> pq;
pq.push(2);
pq.push(1);
pq.push(4);
pq.push(3);
pq.push(7);
pq.push(8);
while (!pq.empty())
{
cout << pq.top() << " ";
pq.pop();
}
cout << endl;
}
int main()
{
test_priority_queue1();
return 0;
}
插入的数据:
运行结果:
打印出来之后可以证明,priority_queue底层是大根堆的结构。
接下来我自己实现以下 priority_queue。
三、priority_queue的模拟实现及思路讲解
设计思路
1、设计两个仿函数less和greater来控制优先队列的底层是大堆还是小堆。大堆用less,小堆用greater。这两个仿函数的功能都只是对插入的值进行比较。(如果比较的对象是比较复杂的自定义类型,也可以对这两个模板进行特化处理。)
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;
}
};
2、 插入用向上调整算法对插入的值进行调整。
void adjust_up(size_t 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;
}
}
}
void push(const T& x)
{
_con.push_back(x);
adjust_up(_con.size() - 1);
}
3、pop()删除的是堆顶元素,用向下调整算法筛选出次大(或小)的值放到堆顶。
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 pop()
{
swap(_con[0], _con[_con.size() - 1]);
_con.pop_back();
adjust_down(0);
}
(向上调整和向下调整的算法讲解在堆排序那章)。
完整代码(其余函数的实现很简单,自己看完整代码):
namespace My_priority_queue
{
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;
}
};
template<class T, class Container = vector<T>, class Compare = less<T>>
class priority_queue
{
public:
void adjust_up(size_t 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;
}
}
}
void push(const T& x)
{
_con.push_back(x);
adjust_up(_con.size() - 1);
}
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 pop()
{
swap(_con[0], _con[_con.size() - 1]);
_con.pop_back();
adjust_down(0);
}
bool empty()
{
return _con.empty();
}
size_t size()
{
return _con.size();
}
const T& top()
{
return _con[0];
}
private:
Container _con;
};
}
测试代码
void test_priority_queue2()
{
// 默认大的优先级高,底层是个大堆
//priority_queue<int> pq;
My_priority_queue::priority_queue<int, vector<int>, My_priority_queue::greater<int>> pq1;
pq1.push(2);
pq1.push(1);
pq1.push(4);
pq1.push(3);
pq1.push(7);
pq1.push(8);
while (!pq1.empty())
{
cout << pq1.top() << " ";
pq1.pop();
}
cout << endl;
My_priority_queue::priority_queue<int, vector<int>, My_priority_queue::less<int>> pq2;
pq2.push(2);
pq2.push(1);
pq2.push(4);
pq2.push(3);
pq2.push(7);
pq2.push(8);
while (!pq2.empty())
{
cout << pq2.top() << " ";
pq2.pop();
}
cout << endl;
}
运行结果:
这样就能做出底层是小堆和大堆的优先队列。