1. priority_queue的介绍
priority_queue文档介绍
翻译:
- 优先队列是一种容器适配器,根据严格的弱排序标准,它的第一个元素总是它所包含的元素中最大的。
- 此上下文类似于堆,在堆中可以随时插入元素,并且只能检索最大堆元素(优先队列中位于顶部的元素)。
- 优先队列被实现为容器适配器,容器适配器即将特定容器类封装作为其底层容器类,queue提供一组特定的成员函数来访问其元素。元素从特定容器的“尾部”弹出,其称为优先队列的顶部。
- 底层容器可以是任何标准容器类模板,也可以是其他特定设计的容器类。容器应该可以通过随机访问迭代器访问,并支持以下操作:
- empty():检测容器是否为空
- size():返回容器中有效元素个数
- front():返回容器中第一个元素的引用
- push_back():在容器尾部插入元素
- pop_back():删除容器尾部元素
- 标准容器类vector和deque满足这些需求。默认情况下,如果没有为特定的priority_queue类实例化指定容器类,则使用vector。
- 需要支持随机访问迭代器,以便始终在内部保持堆结构。容器适配器通过在需要时自动调用算法函数make_heap、push_heap和pop_heap来自动完成此操作。
2. priority_queue的使用
-
优先级队列默认使用vector作为其底层存储数据的容器,在vector上又使用了堆算法将vector中元素构造成堆的结构,因此priority_queue就是堆,所有需要用到堆的位置,都可以考虑使用priority_queue。注意:默认情况下priority_queue是大堆。
-
使用
void test()
{
//默认大堆实现
//priority_queue<int> pq;
//小堆
priority_queue<int, vector<int>, greater<int>> pq;
pq.push(100);
pq.push(1);
pq.push(125);
pq.push(10);
pq.push(0);
while (!pq.empty())
{
cout << pq.top() << " ";
pq.pop();
}
cout << endl;
}
- 利用模拟仿函数实现大堆小堆
#include<list>
#include<stack>
#include<iostream>
#include<vector>
#include<queue>
#include<deque>
#include<functional>
using namespace std;
//仿函数类:重载“()”运算符
template<class T>
struct Greater
{
bool operator()(const T& a, const T& b)
{
return a > b;
}
};
//仿函数类
template<class T>
struct Less
{
bool operator()(const T& a, const T& b)
{
return a < b;
}
};
//添加仿函数类为参数,实现优先级队列的大堆,小堆
//优先级队列默认用vector实现,也支持deque,不支持list
void test()
{
//大堆实现
//priority_queue<int, vector<int>,Less<int>> pq;
//小堆实现
priority_queue<int, vector<int>, Greater<int>> pq;
//priority_queue<int, deque<int>> pq;
//不支持list,list不支持随机访问
pq.push(100);
pq.push(1);
pq.push(125);
pq.push(10);
pq.push(0);
while (!pq.empty())
{
cout << pq.top() << " ";
pq.pop();
}
cout << endl;
}
- 如果在priority_queue中放自定义类型的数据,用户需要在自定义类型中提供> 或者< 的重载。
//自定义类型
class A
{
public:
A(int a=1)
:_a(a)
{}
//大堆比较
bool operator<(const A& a) const
{
return _a < a._a;
}
//小堆比较
bool operator>(const A& a) const
{
return _a > a._a;
}
public:
int _a;
};
void test()
{
//优先级队列存放自定义类型:需要自定义类型支持比较的操作
//priority_queue<A> pq; //大堆
priority_queue<A, vector<A>, greater<A>> pq; //小堆
pq.push(A(1));
pq.push(A(10));
pq.push(A(15));
pq.push(A(2));
pq.push(A(0));
while (!pq.empty())
{
cout << pq.top()._a << " ";
pq.pop();
}
cout << endl;
}
- 优先级队列默认用vector实现,也支持deque,不支持list
void test()
{
//默认实现大堆
//priority_queue<int, vector<int>> pq;
priority_queue<int, deque<int>> pq;
//不支持list,list不支持随机访问
pq.push(100);
pq.push(1);
pq.push(125);
pq.push(10);
pq.push(0);
while (!pq.empty())
{
cout << pq.top() << " ";
pq.pop();
}
cout << endl;
}
3.优先对列的模拟实现
//优先级对列实现
//实现大堆结构
template <class T>
class PriorityQueue
{
public:
//堆:向下调整
void shiftDown()
{
int parent = 0;
int child = 2 * parent + 1;
while (child < arr.size())
{
if (child + 1 < arr.size() && arr[child] < arr[child + 1])
child++;
if (arr[parent] < arr[child])
{
swap(arr[parent], arr[child]);
//更新位置
parent = child;
child = 2 * parent + 1;
}
else
break;
}
}
//向上调整
void shiftUp(int child)
{
int parent = (child - 1) / 2;
while (child > 0)
{
if (arr[parent] < arr[child])
{
swap(arr[parent], arr[child]);
//更新位置
child = parent;
parent = (child - 1) / 2;
}
else
break;
}
}
void push(const T& val)
{
//尾插
arr.push_back(val);
//向上调整
shiftUp(arr.size() - 1);
}
void pop()
{
//交换
swap(arr[0], arr[arr.size() - 1]);
//尾删
arr.pop_back();
//向下调整
shiftDown();
}
T& top()
{
return arr.front();
}
size_t size() const
{
return arr.size();
}
bool empty() const
{
return arr.empty();
}
private:
vector<T> arr;
};
void test()
{
//默认大堆实现
PriorityQueue<int> pq;
//小堆
//priority_queue<int, vector<int>, greater<int>> pq;
pq.push(100);
pq.push(1);
pq.push(125);
pq.push(10);
pq.push(0);
for (int i = 0; i < 1000; i++)
{
pq.push(i);
}
while (!pq.empty())
{
cout << pq.top() << " ";
pq.pop();
}
cout << endl;
}
4.优先对列的模板实现
#include<list>
#include<stack>
#include<iostream>
#include<vector>
#include<queue>
#include<deque>
#include<functional>
using namespace std;
//仿函数类:重载“()”运算符
template<class T>
struct Greater
{
bool operator()(const T& a, const T& b)
{
return a > b;
}
}; template<class T>
struct Less
{
bool operator()(const T& a, const T& b)
{
return a < b;
}
};
//用vector实现优先级队列
//实现大堆结构
template <class T,class Container,class Compare>
class PriorityQueue
{
public:
//堆:向下调整
void shiftDown()
{
int parent = 0;
int child = 2 * parent + 1;
while (child < arr.size())
{
if (child + 1 < arr.size() && cmp(arr[child],arr[child + 1]))
//if (child + 1 < arr.size() && arr[child] < arr[child + 1])
child++;
if (cmp(arr[parent],arr[child]))
//if (arr[parent] < arr[child])
{
swap(arr[parent], arr[child]);
//更新位置
parent = child;
child = 2 * parent + 1;
}
else
break;
}
}
//向上调整
void shiftUp(int child)
{
int parent = (child - 1) / 2;
while (child > 0)
{
if (cmp(arr[parent],arr[child]))
//if (arr[parent] < arr[child])
{
swap(arr[parent], arr[child]);
//更新位置
child = parent;
parent = (child - 1) / 2;
}
else
break;
}
}
void push(const T& val)
{
//尾插
arr.push_back(val);
//向上调整
shiftUp(arr.size() - 1);
}
void pop()
{
//交换
swap(arr[0], arr[arr.size() - 1]);
//尾删
arr.pop_back();
//向下调整
shiftDown();
}
T& top()
{
return arr.front();
}
size_t size() const
{
return arr.size();
}
bool empty() const
{
return arr.empty();
}
private:
//vector<T> arr;
Container arr;
Compare cmp;
};
void test()
{
//PriorityQueue<int, vector<int>,Greater<int>> pq;
PriorityQueue<int, vector<int>, Less<int>> pq;
pq.push(100);
pq.push(1);
pq.push(125);
pq.push(10);
pq.push(0);
while (!pq.empty())
{
cout << pq.top() << " ";
pq.pop();
}
cout << endl;
}