队列stl queue<>_C ++ STL队列容器适配器– std :: queue

本文详细介绍了C++中的STL队列容器适配器std::queue,包括其作为容器适配器的性质,FIFO的工作原理,以及push、pop、empty、back、front和size等操作的用法。还提供了示例程序展示这些功能,并提到了队列的交换操作。
摘要由CSDN通过智能技术生成

队列stl queue<>

In this tutorial you will learn about STL queue container in C++ or std::queue and all functions which it provides.

在本教程中,您将了解C ++或std :: queue中的STL队列容器及其提供的所有功能。

std::queue is a container adaptor. Since queue is a container adaptor, this class uses an encapsulated object. It operates in such way that First in First out (FIFO) type of arrangement. Always elements inserted at back side and deleted from the front side of the queue.

std :: queue是容器适配器。 由于queue是容器适配器,因此此类使用封装的对象。 它以先进先出(FIFO)类型的排列方式运行。 总是将元素插入队列的背面,并从队列的正面删除。

C ++ STL队列容器适配器– std :: queue (C++ STL Queue Container Adaptor – std::queue)

To work with queue we need to include queue header file

要使用队列,我们​​需要包括队列头文件

#include <queue>

#include <队列>

The operations that can be applied on queue are:

可以应用于队列的操作是:

empty(): This is Boolean function. Returns whether the queue is empty or not.

empty():这是布尔函数。 返回队列是否为空。

push(element): This function insert element at back side of the queue.

push(element):此函数在队列背面插入元素。

pop (element):  This function removes the element from queue. Element will be removed from front side.

pop(元素):此函数从队列中删除元素。 元素将从正面移除。

back(): returns the pointer reference of the last element of the queue.

back():返回队列最后一个元素的指针引用。

front(): returns the pointer reference of the first element of the queue.

front():返回队列第一个元素的指针引用。

size(): returns the size of the queue.

size():返回队列的大小。

Example program to show the above functions:

显示上述功能的示例程序:

#include <iostream>
#include <queue>
 
using namespace std;
 
void displayQ( queue <int> q1){
	for(int i=0; i<5; i++){
		cout << q1.front() << " ";
		q1.pop();
	}
	cout << endl;
}
 
int main(){
	queue <int> q1;
	
	for (int i=0; i<5; i++){
		// inserting elements into the queue
		q1.push(i+1);
	}
	
	// calling display function to display elements in the queue
	cout << "the elements in the queue are ";
	displayQ(q1);
	
	cout << "the first element in queue is ";
	cout << q1.front() << endl;
	cout << "the last element in queue is ";
	cout << q1.back() << endl;
	cout << "size of the queue is " ;
	cout << q1.size() << endl;
	
	if (q1.empty()==1){
		cout << "queue is empty" << endl;
	}
	else{
		cout << "queue is not empty " << endl;
	}
	
	return 0;
}

Output

输出量

the elements in the queue are 1 2 3 4 5 the first element in queue is 1 the last element in queue is 5 size of the queue is 5 queue is not empty

队列中的元素是1 2 3 4 5队列中 的第一个元素是1队列中 的最后一个元素 是5 队列的 大小是5 队列不为空

Swap operation of queues:

交换队列操作:

This operation swaps the elements of queue1 to queue2 and elements of queue2 to queue1.

此操作将queue1的元素交换为queue2,将queue2的元素交换为queue1。

Example program to show swap operation:

显示交换操作的示例程序:

#include <iostream>
#include <queue>
 
using namespace std;
 
void displayQ( queue <int> q1){
	for(int i=0; i<5; i++){
		cout << q1.front() << " ";
		q1.pop();
	}
	cout << endl;
}
 
int main(){
	queue <int> q1;
	queue <int> q2;
	
	for (int i=0; i<5; i++){
		// inserting elements into the queue 1
		q1.push(i+1);
	}
	
	cout << "elements 1,2,3,4,5 inserted into queue 1" << endl;
	for (int i=0; i<5; i++){
		// inserting elements into the queue 2
		q2.push(i*10);
	}
	
	cout << "elements 0,10,20,30,40 inserted into queue 2" << endl;
	q1.swap(q2);
	cout << "contents after swapping operation" << endl;
	
	cout << "elements of q1 are " << endl;
	displayQ (q1);
	
	cout << "elements of q2 are " << endl;
	displayQ (q2);
	
	return 0;
}

Output

输出量

elements 1,2,3,4,5 inserted into queue 1 elements 0,10,20,30,40 inserted into queue 2 contents after swapping operation elements of q1 are 0 10 20 30 40 elements of q2 are 1 2 3 4 5

插入队列1的元素1,2,3,4,5插入队列1的 元素0,10,20,30,40 交换 q1的 操作 元素 后的内容 0 10 20 30 40 q2的元素为 1 2 3 4 5

Comment below if you have any doubts related to above C++ STL queue container or std::queue.

如果您对以上C ++ STL队列容器或std :: queue有任何疑问,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2017/10/stl-queue.html

队列stl queue<>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值