队列:允许从队列的两端对数据进行操作。队列从队尾加入数据,从队首删除数据。
queue类 构造函数
queue() 创建一个空队列
queue类 操作
bool empty() const;
T& front();
const T& front() const;
void pop();
void push(const T& item);
int size() const ;
8.2 基数排序
#ifndef SORT_H
#define SORT_H
#include
#include
#include
using namespace std;
void distribute(const vector
& v , queue
digitQueue[],int power)
{
int i ;
for (i = 0 ;i
{
digitQueue[(v[i]/power)%10].push(v[i]); //利用power关联的数位把向量的元素分配到10个队列
}
}
void collect(queue
digitQueue[],vector
& v )
{
//从各个队列中收集数据,并复制回向量
int i = 0 , digit;
for(digit = 0; digit <10;digit ++)
{
while(!digitQueue[digit].empty())
{
v[i] = digitQueue[digit].front();
digitQueue[digit].pop();
i ++ ;
}
}
}
void radixSort(vector
& v , int d )//用基数排序法排序,v中整数最大的为d位
{
int i ;
int power = 1 ;
queue
digitQueue[10];
for (i = 0;i
{
distribute(v,digitQueue,power);
collect(digitQueue,v);
power *= 10 ;
}
}
#endif
8.3 实现miniQueue类
miniQueue使用list对象来存储数据,原因为:向量定义了从序列的队尾访问数据的操作,却不能有效地从队首删除数据。list类front方法能够从队首访问序列数据,pop_front 方法可以有效地删除队首数据。
STL queue类的实现方法::STL queue类的设计和栈类的设计几乎一样。程序员可以选择内部存储结构来保存队列元素。我们可以选择任何支持size()、empty()、push_back()、front()、和pop_front()操作的容器,因为这些操作是实现队列所必须的接口。表就是这样的一种容器,另一种是双端队列。STL使用双端队列作为保存队列元素的容器。
注意::用表还是双端队列?list类在加入数据项时,每个数据项都是独立的元素。当pop()方法删除元素时,list类将释放被删除元素占用的内存,在以后加入操作时,可以再次分配内存。相反的,双端队列按块分配内存,在加入和删除数据时进行的操作和向量类似。一般来说,用双端队列实现的队列要快些。但是,程序员还是应该从内存开销和速度两方面综合考虑,然后决定在应用程序中到底使用哪一个更好。
8.5 用数组实现队列
表容器类提供了能够直接实现队列的成员函数。表存储使得队列按需增长,因此不会出现“队列满“的情况。在本节,我们将设计一种有界队列容器类bqueue。有界队列是一种修改过的队列,她只具有某些特殊的应用。bqueue类通过数组来保存数据元素,并声明了用来引用队首和队尾元素的数组下标。从而实现了队列结构。数组能够容纳固定数量的元素,这个数量定义在常量MAXQSIZE里。
我们把队列看成一种循环序列。环形的队列上有一系列的空余位置,数据元素按照顺时针方向加入到队列中来。删除时,删除qfront指定位置上的元素;添加时,新数据保存到qback指定的位置上。
为了使下标qfront和qback在数组内循环起来,我们使用%得到除以MAXQSIZE的余数。每次加入或者删除数据时,相应的下标变量(qfront或qback)加一 ,然后用该变量除以数组长度,把余数赋回给该变量。当下标移出数组末尾时,他的值会重置到数组的起始端:
向前移动qback:qback = (qback+1)%MAXQSIZE;
向前移动qfront:qfront=(qfront+1)%MAXQSIZE ;
8.6 优先级队列
队列是提供先进先出顺序的数据结构。在删除数据时,选取队列中的第一个元素。但有时,应用程序需要用到队列修改后的一种版本,要求从队列中弹出数据时能够按照队列元素优先级的顺序进行,而不是按照他们进入队列的顺序。这种数据结构叫做优先级队列。他在删除数据时,总是在数据元素中挑选优先级最大的那个。每个元素的优先级是由一些外部标准确定的。
优先级队列的抽象模型并不把数据存储结构看成是顺序容器。元素在进入队列时没有任何限制,但在退出时却有一个标准。
STL标准模板库用priority_queue类来实现优先级队列ADT。数据类型T必须实现关系运算符 < .
priority_queue
template
,
class Pred = less
>
class priority_queue {
public:
typedef Cont::allocator_type
allocator_type;
typedef Cont::value_type
value_type;
typedef Cont::size_type
size_type;
explicit
priority_queue(const Pred& pr = Pred(),
const allocator_type& al = allocator_type());
priority_queue(const value_type *first, const value_type *last,
const Pred& pr = Pred(), const allocator_type& al = allocator_type());
bool
empty() const;
size_type
size() const;
allocator_type
get_allocator() const;
value_type&
top();
const value_type&
top() const;
void
push(const value_type& x);
void
pop();
protected:
Cont
c;
Pred
comp;
};
The template class describes an object that controls a varying-length sequence of elements. The object allocates and frees storage for the sequence it controls through a protected object named c
, of class Cont
. The type T
of elements in the controlled sequence must match value_type
.
The sequence is ordered using a protected object named comp
. After each insertion or removal of the top element (at position zero), for the iterators P0
and Pi
designating elements at positions 0
and i
, comp(*P0, *Pi)
is false. (For the default template parameter less
, the top element of the sequence compares largest, or highest priority.)
An object of class Cont
must supply random-access iterators and several public members defined the same as for deque
and vector
(both of which are suitable candidates for class Cont
). The required members are:
typedef T value_type;
typedef T0 size_type;
Cont(const A& al);
Cont(InIt first, InIt last, const allocator_type& al);
bool empty() const;
size_type size() const;
allocator_type get_allocator() const;
const value_type& front() const;
value_type& front();
void push_back(const value_type& x);
void pop_back();
Here, T0
is an unspecified type that meets the stated requirements.
默认情况下优先级队列假定凡具有最大值的元素也具有最大的优先级。
注意::优先级队列的次序:当我们从优先级队列中删除数据时,可能有几个元素具有相同的优先级。在这种情况下,可以用队列的方法处理这些元素。
.STL priority_queue类是通过使用堆来实现优先级队列的。
class jobRequest
{
public:
enum staff {Clerk = 0 ,Supervisor = 1 , Manager = 2 , President = 3};
jobRequest();
//use >> to init
jobRequest(staff stat , int jid, int jtime);
staff getStatus() const ;
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
在类声明之外,实现getStatus()的时候,返回类型必须描述成jobRequest::staff。因为编译器首先需要返回类型信息,然后才能通过域运算符发现getStatus()是jobRequest类的成员函数。