注意 : 以下所有文档都来源此网站 : http://cplusplus.com/
一、priority_queue 的介绍
priority_queue 文档的介绍:https://cplusplus.com/reference/queue/priority_queue/
1. 优先队列是一种容器适配器,根据严格的弱排序标准,它的第一个元素总是它所包含的元素中最大的。
2. 此上下文类似于堆,在堆中可以随时插入元素,并且只能检索最大堆元素(优先队列中位于顶部的元素)。
3. 优先队列被实现为容器适配器,容器适配器即将特定容器类封装作为其底层容器类,queue提供一组特定的成员函数来访问其元素。元素从特定容器的“尾部”弹出,其称为优先队列的顶部。
4. 底层容器可以是任何标准容器类模板,也可以是其他特定设计的容器类。容器应该可以通过随机访问迭代器访问,并支持以下操作:
empty():检测容器是否为空
size():返回容器中有效元素个数
front():返回容器中第一个元素的引用
push_back():在容器尾部插入元素
pop_back():删除容器尾部元素
5. 标准容器类vector和deque满足这些需求。默认情况下,如果没有为特定的priority_queue类实例化指定容器类,则使用vector。
6. 需要支持随机访问迭代器,以便始终在内部保持堆结构。容器适配器通过在需要时自动调用算法函数make_heap、push_heap和pop_heap来自动完成此操作。
二、priority_queue 常用接口的介绍
优先级队列默认使用vector作为其底层存储数据的容器,在vector上又使用了堆算法将vector中元素构造成堆的结构,因此priority_queue就是堆,所有需要用到堆的位置,都可以考虑使用priority_queue。
注:默认情况下 priority_queue 是大堆。
函数声明
| 接口说明 |
构造一个空的优先级队列 | |
检测优先级队列是否为空,是返回true,否则返回 false | |
返回优先级队列中最大(最小元素),即堆顶元素 | |
在优先级队列中插入元素x | |
删除优先级队列中最大(最小)元素,即堆顶元素 |
三、priority_queue 的模拟实现
1.下面是负责实现接口的文件 priority_queue.hpp 文件的代码:
#pragma once
#include <iostream>
#include <vector>
using namespace std;
namespace hk
{
// 重载operator()
// 类对象像函数一样使用
template <class T>
class less
{
public:
bool operator()(const T& L, const T& R)
{
return L < R;
}
};
template <class T>
class greater
{
public:
bool operator()(const T& L, const T& R)
{
return L > R;
}
};
}
namespace HK
{
template <class T, class container = vector<T>, class compare = hk::greater<T>>
class priority_queue
{
public:
priority_queue()
{}
template<class InputIterator>
priority_queue(InputIterator first, InputIterator last)
{
while (first != last)
{
this->_con.push_back(*first);
first++;
}
for (int i = (_con.size() - 1 - 1) / 2; i >= 0; i--)
{
adjust_down(i);
}
}
// 默认大堆
void adjust_up(size_t child)
{
compare com;
size_t parent = (child - 1) / 2;
while (child > 0)
{
//if (_con[child] > _con[parent])
//if (_con[parent] < _con[child])
if (com(_con[parent], _con[child]))
{
swap(_con[child], _con[parent]);
child = parent;
parent = (child - 1) / 2;
}
else
{
break;
}
}
}
void push(const T& x)
{
// 先尾插
this->_con.push_back(x);
// 再向上调整
this->adjust_up(this->_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() && _con[child + 1] > _con[child])
//if (child + 1 < _con.size() && _con[child] < _con[child + 1])
if (child + 1 < _con.size() && com(_con[child], _con[child + 1]))
{
child = child + 1;
}
//if (_con[child] > _con[parent])
//if (_con[parent] < _con[child])
if (com(_con[parent], _con[child]))
{
swap(_con[child], _con[parent]);
parent = child;
size_t child = parent * 2 + 1;
}
else
{
break;
}
}
}
void pop()
{
// 先交换头尾
swap(_con.front(), _con.back());
// 再删除尾
this->_con.pop_back();
// 向下调整
adjust_down(0);
}
const T& top() const
{
return this->_con.front();
}
bool empty() const
{
return this->_con.empty();
}
size_t size() const
{
return this->_con.size();
}
private:
container _con;
};
}
2. 下面是负责测试所模拟实现的接口 Test.cpp文件的代码:
#include "priority_queue.hpp"
void Test_priority_queue()
{
HK::priority_queue<int> pq;
//pq.push(1);
//pq.push(3);
//pq.push(4);
//pq.push(5);
//pq.push(2);
pq.push(3);
pq.push(1);
pq.push(2);
pq.push(5);
pq.push(0);
pq.push(1);
while (!pq.empty())
{
cout << pq.top() << " ";
pq.pop();
}
cout << endl;
int a[] = { 5, 8, 6, 9, 7, 1, 5, 9 };
HK::priority_queue<int> pq1(a, a + sizeof(a) / sizeof(int));
while (!pq1.empty())
{
cout << pq1.top() << " ";
pq1.pop();
}
cout << endl;
}
int main()
{
Test_priority_queue();
return 0;
}