C++ 标准模板库STL 优先级队列 priority_queue 使用方法与应用介绍(一) (二)

2014-11-24 02:27:58 · 作者: · 浏览: 5
首和队尾元素的操作,因此 priority_queue 优先队列容器也不提供迭代器,对其他任意位置处的元素进行直接访问操作。使用时,一般用 priority_queue 的形式进行具现, T 是优先队列元素的一个具现类型。

创建 priority_queue 对象
使用 priority_queue 队列之前,要先利用构造函数生成一个优先对象,才可进行元素的入队、出对、取队首及队尾等操作。
1. priority_queue()
默认的构造函数,创建一个空的 priority_queue 对象。例如,下面一行代码使用默认的 vector 为底层容器,创建了一个空的优先队列对象 pq ,数据元素为 int 类型。
priority_queue pq;

2. priority_queue(const priority_queue&)
复制构造函数,用一个优先队列对象创建新的优先队列对象。例如,下面一行代码利用 priority_queue 对象 pq1 ,创建一个以双向链表为底层容器的 priority_queue 对象 pq2 。
// priority_queue > pq1;
priority_queue > 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
#include
using namespace std;
void test_empty()
{
priority_queue 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 mypq;

mypq.push(30);
mypq.push(100);
mypq.push(25);
mypq.push(40);

cout << "Popping out elements...";
while (!mypq.empty())
{
cout << " " << mypq.top();
mypq.pop();
}
cout << endl;
}//Popping out elements... 100 40 30 25
void test_top()
{
priority_queue mypq;

mypq.push("how");
mypq.push("are");
mypq.push("you");

cout << "mypq.top() is now:--->>> " << mypq.top() << endl;
}//mypq.top() is now:--->>> you
int main()
{
test_empty();
cout<<"\n***********************************************\n";
test_pop();
cout<<"\n***********************************************\n";
test_top();
cout<<"\n***********************************************\n";
priority_queue q;

// insert three elements into the priority queue
q.push(66.6);
q.push(22.2);
q.push(44.4);

// read and print two elements
cout << q.top() << ' ';
q.pop();
cout << q.top() << endl;
q.pop();

// insert three more elements
q.push(11.1);
q.push(55.5);
q.push(33.3);

// skip one element
q.pop();

// pop and print remaining elements
while (!q.empty())
{
cout << q.top() << ' ';
q.pop();
}
cout << endl;
}
/******************
运行结果:
total: 5050

***********************************************
Popping out elements... 100 40 30 25

***********************************************
mypq.top() is now:--->>> you

***********************************************
66.6 44.4
33.3 22.2 11.1

Process returned 0 (0x0) execution time : 0.055 s
Press any key to continue.

********************/

#include
#include
using namespace std;
void test_empty()
{
priority_queue 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 mypq;

mypq.push(30);
mypq.push(100);
mypq.push(25);
mypq.push(40);

cout << "Popping out elements...";
while (!mypq.empty())
{
cout << " " << mypq.top();
mypq.pop();
}
cout << endl;
}//Popping out elements... 100 40 30 25
void test_top()
{
priority_queue mypq;

mypq.push("how");
mypq.push("are");
mypq.push("you");

cout << "mypq.top() is now:--->>> " << mypq.top() << endl;
}//mypq.top() is now:--->>> you
int main()
{
test_empty();
cout<<"\n***********************************************\n";
test_pop();
cout<<"\n***********************************************\n";
test_top();
cout<<"\n***********************************************\n";
priority_queue q;

// insert three elements into the priorit