c ++stl队列使用
C++ provides the awesome feature of STL where we can use the basic ADTs without knowing the code behind the implementations. STL helps a lot to use ADTs efficiently without actually implementing them. This article provides basic knowledge to represent a Queue using C++ STL.
C ++提供了STL的强大功能,我们可以在不了解实现背后的代码的情况下使用基本ADT。 STL可以有效地使用ADT,而无需实际实施它们。 本文提供了使用C ++ STL表示队列的基本知识。
Queue:
队列:
A queue is the data structure where the order of elements is important and the queue is maintained as FIFO (First In First Out).
队列是一种数据结构,其中元素的顺序很重要,并且该队列被保持为FIFO(先进先出) 。
The basic ADT operations for queue are...
队列的基本ADT操作是...
EnQueue (int x): to enqueuer an element at the rear end
EnQueue(int x) :在后端加入一个元素
int DeQueue(): to delete an element from the front end and returns the element deleted
int DeQueue() :从前端删除元素并返回已删除的元素
bool empty(): a Boolean function to check whether the queue is empty or not
bool empty() :一个布尔函数,用于检查队列是否为空
int size(): returns no of elements presented in the queue
int size() :不返回队列中显示的元素
在STL中排队 (Queue in STL)
The basic ADT operations for queue are available in the STL.
STL中提供了队列的基本ADT操作。
Queue is the container and the available functions for implanting the ADT operations are mentioned following:
队列是容器,下面提到用于植入ADT操作的可用功能:
empty() – Returns 1 if the queue is empty else 0
empty() –如果队列为空,则返回1,否则返回0
size() – Returns the size of the queue
size() –返回队列的大小
front() – Returns a reference to the first element of the queue (front end)
front() –返回对队列第一个元素( 前端 )的引用
back() – Returns a reference to the last element of the queue (rear end)
back() –返回对队列最后一个元素( 后端 )的引用
push(g) – Adds the element ‘g’ at the end of the queue (conventional EnQueue(g) ADT operation)
push(g) –在队列末尾添加元素“ g”(常规EnQueue(g) ADT操作)
pop() – Deletes the first element of the queue ( similar to DeQueue(), but it only deletes, doesn’t return anything.)
pop() –删除队列的第一个元素(类似于DeQueue() ,但它仅删除,不返回任何内容。)
So to complete the DeQueue operation we need to write
因此,要完成DeQueue操作,我们需要编写
int item= front();
int item = front();
pop();
pop();
return item;
归还物品;
To declare queue in C++ STL:
在C ++ STL中声明队列:
queue<datatype> queue_name;
Example:
例:
queue<int> q; //a queue of integers
C ++程序说明STL中的队列 (C++ program to illustrate queue in STL)
#include <iostream>
#include <queue> // include header file for 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); //push(int x) function for EnQueueing
cin>>x;
}
cout << "The size of the queue is : "<<q.size()<<endl; //size() function
cout<<"The first element that entered the queue is : "<<q.front()<<endl; //front() function
cout<<"The last element that entered the queue is : "<<q.back()<<endl; //back() function
cout<<"DeQueuing......."<<endl;
while(!q.empty()){
item=q.front(); // DeQueing
cout<<item<<" ";
q.pop();
}
cout<<"\n executed successfully\n";
return 0;
}
Output
输出量
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
c ++stl队列使用