stl:queue 源码_C ++ STL中的queue :: front()和queue :: back()

stl:queue 源码

In C++ STL, Queue is a type of container that follows FIFO (First-in-First-Out) elements arrangement i.e. the elements which insert first will be removed first. In queue, elements are inserted at one end known as "back" and are deleted from another end known as "front".

在C ++ STL中,队列是遵循FIFO(先进先出)元素排列的一种容器,即,首先插入的元素将被首先删除。 在队列中,元素被插入称为“ back”的一端,并从称为“ front”的另一端删除。

1)C ++ STL queue :: front()函数 (1) C++ STL queue::front() function)

The function front() returns the reference to the first element in the queue i.e. the oldest element in the queue, so it is used to get the first element from the front of the list of a queue.

函数front()返回对队列中第一个元素(即队列中最旧的元素)的引用,因此它用于从队列列表的开头获取第一个元素。

Syntax:

句法:

    queue_name.front();

2)C ++ STL queue :: back()函数 (2) C++ STL queue::back() function)

The function back() returns the reference to the last element in the queue i.e. the newest element in the queue, so it is used to get the first element from the back of the list of a queue.

函数back()返回对队列中最后一个元素(即队列中的最新元素)的引用,因此它用于从队列列表的后面获取第一个元素。

Syntax:

句法:

    queue_name.back();

Program:

程序:

// cpp program for queue implementation 
// Example of front() and back()
#include <iostream>
#include <queue>
using namespace std;

//Main function
int main() 
{
	// declaring an empty queue
	queue<int> Q;

	//inserting elements
	Q.push(10);
	Q.push(20);
	Q.push(30);
	Q.push(40);
	Q.push(50);

	cout<<"First element of the queue: "<<Q.front()<<endl;
	cout<<"Last element of the queue : "<<Q.back()<<endl;

	//removing two elements 
	Q.pop();
	Q.pop();

	cout<<"After removing two elements..."<<endl;
	cout<<"First element of the queue: "<<Q.front()<<endl;
	cout<<"Last element of the queue : "<<Q.back()<<endl;

	return 0;
}

Output

输出量

First element of the queue: 10 
Last element of the queue : 50 
After removing two elements... 
First element of the queue: 30 
Last element of the queue : 50 


翻译自: https://www.includehelp.com/stl/queue-front-and-queue-back.aspx

stl:queue 源码

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值