C ++标准模板库(STL)中的队列
C++提供了STL的强大功能,我们可以使用基本的ADT,而无需了解实现背后的代码。STL可以有效地使用ADT,而无需实际实现它们。本文提供了使用C++STL表示队列的基本知识。
队列:
甲队列是数据结构,其中元素的顺序是重要的,该队列保持为FIFO(先入先出)。
队列的基本ADT操作是...
入列(INTX):在enqueuer的元件后结束
intDeQueue():从前端删除元素并返回已删除的元素
boolempty():检查队列是否为空的布尔函数
intsize():不返回队列中显示的元素
在STL中排队
STL中提供了队列的基本ADT操作。
队列是容器,下面将介绍用于植入ADT操作的可用功能:
empty()–如果队列为空,则返回1,否则返回0
size()–返回队列的大小
front()–返回对队列第一个元素(前端)的引用
back()–返回对队列最后一个元素(后端)的引用
push(g)–在队列末尾添加元素“g”(常规EnQueue(g)ADT操作)
pop()–删除队列的第一个元素(类似于DeQueue(),但是仅删除,不返回任何内容。)
因此,要完成DeQueue操作,我们需要编写
intitem=front();
pop();
归还物品;
在C++STL中声明队列:
queue<datatype> queue_name;
示例
queue<int> q; //整数队列
C++程序说明STL中的队列
#include <iostream>
#include <queue> //包括STL的头文件
using namespace std;
int main() {
queue <int> q;
int x,item;
cout<<"enter integers to EnQueue and 0 to stop EnQuing"<<endl;
cin>>x;
while(x){
q.push(x); //用于EnQueueing的push(intx)函数
cin>>x;
}
cout << "The size of the queue is : "<<q.size()<<endl; //size()函数
cout<<"The first element that entered the queue is : "<<q.front()<<endl; //front()函数
cout<<"The last element that entered the queue is : "<<q.back()<<endl; //back()函数
cout<<"DeQueuing......."<<endl;
while(!q.empty()){
item=q.front(); //德奎
cout<<item<<" ";
q.pop();
}
cout<<"\n executed successfully\n";
return 0;
}输出结果
enter integers to EnQueue and 0 to stop EnQuing 12 4 45 3 6 7 0 The size of the queue is : 6 The first element that entered the queue is : 12 The last element that entered the queue is : 7 DeQueuing....... 12 4 45 3 6 7 executed successfully