c ++stl队列使用_C ++标准模板库(STL)中的队列

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操作是...

  1. EnQueue (int x): to enqueuer an element at the rear end

    EnQueue(int x) :在后端加入一个元素

  2. int DeQueue(): to delete an element from the front end and returns the element deleted

    int DeQueue() :从前端删除元素并返回已删除的元素

  3. bool empty(): a Boolean function to check whether the queue is empty or not

    bool empty() :一个布尔函数,用于检查队列是否为空

  4. 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操作的可用功能:

  1. empty() – Returns 1 if the queue is empty else 0

    empty() –如果队列为空,则返回1,否则返回0

  2. size() – Returns the size of the queue

    size() –返回队列的大小

  3. front() – Returns a reference to the first element of the queue (front end)

    front() –返回对队列第一个元素( 前端 )的引用

  4. back() – Returns a reference to the last element of the queue (rear end)

    back() –返回对队列最后一个元素( 后端 )的引用

  5. push(g) – Adds the element ‘g’ at the end of the queue (conventional EnQueue(g) ADT operation)

    push(g) –在队列末尾添加元素“ g”(常规EnQueue(g) ADT操作)

  6. 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操作,我们需要编写

  1. int item= front();

    int item = front();

  2. pop();

    pop();

  3. 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


翻译自: https://www.includehelp.com/stl/queue.aspx

c ++stl队列使用

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值