C++ 标准模板库STL 优先级队列 priority_queue 使用方法与应用介绍(一)
priority_queue
Priority queues are a type of container adaptors, specifically designed such that its first element is always the greatest of the elements it contains, according to some strict weak ordering condition.
This context is similar to a heap where only the max heap element can be retrieved (the one at the top in the priority queue) and elements can be inserted indefinitely.
Priority queues are implemented as container adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are popped from the "back" of the specific container, which is known as the top of the priority queue.
The underlying container may be any of the standard container class templates or some other specifically designed container class. The only requirement is that it must be accessible through random access iterators and it must support the following operations:
front()
push_back()
pop_back()
Therefore, the standard container class templates vector and deque can be used. By default, if no container class is specified for a particular priority_queue class, the standard container class template vector is used.
Support for random access iterators is required to keep a heap structure internally at all times. This is done automatically by the container adaptor by calling the algorithms make_heap, push_heap and pop_heap when appropriate.
In their implementation in the C++ Standard Template Library, priority queues take three template parameters:
1
2
template < class T, class Container = vector<T>,
class Compare = less<typename Container::value_type> > class priority_queue;
Where the template parameters have the following meanings:
T: Type of the elements.
Container: Type of the underlying container object used to store and access the elements.
Compare: Comparison class: A class such that the expression comp(a,b), where comp is an object of this class and a and b are elements of the container, returns true if a is to be placed earlier than b in a strict weak ordering operation. This can either be a class implementing a function call operator or a pointer to a function. This defaults to less<T>, which returns the same as applying the less-than operator (a<b).
The priority_queue object uses this expression when an element is inserted or removed from it (using push orpop, respectively) to grant that the element popped is always the greatest in the priority queue.
In the reference for the priority_queue member functions, these same names (T, Container and Compare) are assumed for the template parameters.
Member functions
(constructor)
Construct priority queue (public member function )
empty
Test whether container is empty (public member function)
size
Return size (public member function)
top
Access top element (public member function)
push
Insert element (public member function)
pop
Remove top element (public member function)
priority_queue <Elem> c
创建一个空的queue 。
注:priority_queue构造函数有7个版本,请查阅MSDN
c.top()
返回队列头部数据
c.push(elem)
在队列尾部增加elem数据
c.pop()
队列头部数据出队
c.empty()
判断队列是否为空
c.size()
返回队列中数据的个数
优先队列容器也是一种从一端入队,另一端出对的队列。不同于一般队列的是,队列中最大的元素总是位于队首位置,因此,元素的出对并非按照先进先出的要求,将最先入队的元素出对,而是将当前队列中的最大元素出对。 C++ STL 优先队列的泛化,底层默认采用 vector 向量容器,使得队列容器的元素可做数组操作,从而应用堆算法找出当前队列最大元素,并将它调整到队首位置,确保最大元素出队。堆算法(heap algorithm) 具有 nLog(n) 阶的算法时间复杂度,此外,优先队列也可看作容器适配器,将底层的序列容器 vector 转换为优先队列priority_queue.
priority_queue 优先队列容器的 C++ 标准头文件也是 queue ,需要用宏语句 "#include <queue>" 包含进来。
同样是因为仅需取队首和队尾元素的操作,因此 priority_queue 优先队列容器也不提供迭代器,对其他任意位置处的元素进行直接访问操作。使用时,一般用 priority_queue<T> 的形式进行具现, T 是优先队列元素的一个具现类型。
创建 priority_queue 对象
使用 priority_queue 队列之前,要先利用构造函数生成一个优先对象,才可进行元素的入队、出对、取队首及队尾等操作。
1. priority_queue()
默认的构造函数,创建一个空的 priority_queue 对象。例如,下面一行代码使用默认的 vector 为底层容器,创建了一个空的优先队列对象 pq ,数据元素为 int 类型。
priority_queue<int> pq;
2. priority_queue(const priority_queue&)
复制构造函数,用一个优先队列对象创建新的优先队列对象。例如,下面一行代码利用 priority_queue 对象 pq1 ,创建一个以双向链表为底层容器的 priority_queue 对象 pq2 。
// priority_queue<int, list<int> > pq1;
priority_queue<int, list<int> > pq2(pq1);
元素入队
优先队列容器的元素入队函数也是 push 函数,它调用堆算法函数将入队的元素移至队列堆中的正确位置,保证队列优先级高的元素始终位于队首。优先队列也不预设固定的大小,因此 push 函数不判断队列空间是否已满,都将元素放入队列。push 函数不会返回元素入队是否成功的信息。
void push(const value_type& x)
元素出对
优先队列容器的元素出对函数为 pop 函数,将优先级最高的元素删掉。该函数不判断队列是否已为空,都试图将队首元素删除。一般要先判断队列不为空,才进行元素出对操作。
取队首元素
优先队列容器的 top 函数,可用来读取队首元素,即优先级最高的元素。这个函数实际是调用了底层容器的 front 函数。需要注意的是,优先队列容器并不提供获取队尾元素的函数。如下是 top 函数的使用原型。
const value_type& top() const
队列非空判断
优先队列的操作基本都要使用 empty 函数,判断入队和出对的优先队列是否为空,再作下一步的操作。如下是 empty 函数的使用原型。
bool empty()
[cpp]
#include <iostream>
#include <queue>
using namespace std;
void test_empty()
{
priority_queue<int> mypq;
int sum (0);
for (int i=1;i<=100;i++) mypq.push(i);
while (!mypq.empty())
{
sum += mypq.top();
mypq.pop();
}
cout << "total: " << sum << endl;
}//total: 5050
void test_pop()
{
priority_queue<int> mypq;
mypq.push(30);
mypq.push(100);
mypq.push(25);
mypq.push(40);
cout << "Popping out elements...";
while (!mypq.empty())
{
cout << "
补充:软件开发 , C++ ,