使用
priority_queue也是queue,所以其只允许在队头出数据,在队尾入数据,priority使得该队列有了权值的概念,priority_queue会对入到其内的元素按照元素权值进行排列。
#include<iostream>
using namespace std;
void test_priority_queue()
{
int arr[] = { 9,6,7,5,3,2,1 };
priority_queue<int> pq(arr, arr + 7);
cout << "size=" << pq.size() << endl;
while (!pq.empty())
{
cout << pq.top() << " ";//9 7 6 5 3 2 1 默认大的优先级高
pq.pop();
}
cout << endl;
}
int main()
{
test_priority_queue();
return 0;
}
输出:
如果想变成小的优先级高,该怎么办呢?
---可以使用仿函数
#include<iostream>
using namespace std;
void test_priority_queue()
{
int arr[] = { 9,6,7,5,3,2,1 };
priority_queue<int, vector<int>, greater<int>> pq(arr, arr + 7);
//这里的greater就是一个仿函数
cout << "size=" << pq.size() << endl;
while (!pq.empty())
{
cout << pq.top() << " ";//1 2 3 5 6 7 9
pq.pop();
}
cout << endl;
}
int main()
{
test_priority_queue();
return 0;
}
输出:
我们已经知道了可以使用仿函数来改变priority_queue的优先级,具体是如何做到的的,我们可以通过实现一个priority_queue来更深入的了解它。
实现
namespace my_priqueue
{
template<class T, class Container = vector<T>, class Compare = less<T>>
class priority_queue
{
public:
void AdjustUp(int child)
{
Compare com;
int parent = (child - 1) / 2;
while (child > 0)
{
//if(_con[child]<_con[parent])
if(com(_con[parent], _con[child]))//父亲大于孩子,则孩子往上调
{
swap(_con[child], _con[parent]);
child = parent;
parent = (child - 1) / 2;
}
else
{
break;
}
}
}
void AdjustDown(int root)
{
int parent = root;
int child = parent * 2 + 1;
Compare com;
while (child < _con.size())
{
// 选出左右孩子中大的那一个
if (child + 1 < _con.size() && com(_con[child],_con[child+1]))
{
++child;
}
//if (_con[child] > _con[parent])
if(com(_con[parent],_con[child]))
{
swap(_con[child], _con[parent]);
parent = child;
child = parent * 2 + 1;
}
else
{
break;
}
}
}
void push(const T& x)
{
//O (logN)
_con.push_back(x);
AdjustUp(_con.size() - 1);
}
void pop()
{
swap(_con[0], _con[_con.size() - 1]);
_con.pop_back();
//O (logN)
AdjustDown(0);
}
T& top()
{
return _con[0];
}
size_t size()
{
return _con.size();
}
bool empty()
{
return _con.empty();
}
//它的对象可以像函数一样的去使用
template<class T>
struct less {
bool operator()(const T& x1, const T& x2) { return x1 < x2; }
};
template<class T>
struct greater
{
bool operator()(const T& x1, const T& x2) { return x1 > x2; }
};
private:
Container _con;
};
void test_priority_queue()
{
priority_queue<int> pq;
//priority_queue<int,vector<int>,greater<int>> pq;
pq.push(3);
pq.push(1);
pq.push(9);
pq.push(4);
pq.push(2);
while (!pq.empty())
{
cout << pq.top() << " ";
pq.pop();
}
cout << endl;
}
}
直接看代码可能有点懵,这里我们划分一下它的结构,首先我们看my_priority类里面定义的各个函数的具体功能:
Adjustup() 向上调整:队列中的数据是一个一个插入的,每插入一个数据,调用一下Adjustup()来确保父亲节点永远小于孩子节点。
Adjustdown() 向下调整:由于队列只能从头部出数据,但是头删再排序消耗比较大,所以我们选择将头部数据与尾部数据进行交换,在通过向下调整算法来保证队列中的数据是一个小堆。
剩下的函数就好理解了
push() 队列尾入数据
pop() 队头出数据
top() 获取队头数据
size() 获取队列中元素的个数
empty() 判断队列是否为空
然后我们再单独介绍一下里面使用到的仿函数
namespace my_priqueue
{
//它的对象可以像函数一样的去使用
template<class T>
struct less
{
bool operator()(const T& x1, const T& x2){return x1 < x2;}
};
template<class T>
struct greater
{
bool operator()(const T& x1, const T& x2){return x1 > x2;}
};
}
int main()
{
less<int> le;
cout << le(3, 4) << endl;//3 < 4 返回1
greater<int> ge;
cout << ge(3, 4) << endl;//3 <4 返回0
return 0;
}