C ++ STL中的queue :: front()和queue :: back()
在本文中,我们将讨论C++STL中queue::front()和queue::back()函数的工作,语法和示例。
C++STL中的队列是什么?
队列是C++STL中定义的简单序列或数据结构,它以FIFO(先进先出)的方式插入和删除数据。队列中的数据以连续方式存储。元素将插入到末尾,并从队列的开头删除。在C++STL中,已经有一个预定义的队列模板,该模板以类似于队列的方式插入和删除数据。
什么是queue::front()?
queue::front()是C++STL中的内置函数,该声明在front()直接引用队列容器中最旧的元素。
就像上面给定的图中一样,head(即1)是已在队列中输入的第一个元素,而tail(即-4)是已在队列中输入的最后一个或最近的元素
语法
myqueue.front();
此函数不接受任何参数
返回值
此函数返回对元素的引用,该元素首先插入队列容器。
示例
Input: queue<int> myqueue = {10, 20, 30, 40};
myqueue.front();
Output:
Front element of the queue = 10示例
#include <iostream>
#include <queue>
using namespace std;
int main(){
queue<int> Queue;
Queue.push(10);
Queue.push(20);
Queue.push(30);
Queue.push(40);
Queue.push(40);
cout<<"Element in front of a queue is: "<<Queue.front();
return 0;
}输出结果
如果我们运行上面的代码,它将生成以下输出-
队列前面的元素是:10
什么是queue::back()?
queue::back()是C++STL中的内置函数,在back()直接引用队列容器中最新的元素。
语法
myqueue.back();
此函数不接受任何参数
返回值
此函数返回对最后插入队列容器的元素的引用。
示例
Input: queue<int> myqueue = {10, 20 30, 40};
myqueue.back();
Output:
Back element of the queue = 40示例
#include <iostream>
#include <queue>
using namespace std;
int main(){
queue<int> Queue;
Queue.push(10);
Queue.push(20);
Queue.push(30);
Queue.push(40);
Queue.push(50);
cout<<"Elements at the back of the queue is: "<<Queue.back();
return 0;
}输出结果
如果我们运行上面的代码,它将生成以下输出-
Elements at the back of the queue is: 50
热门推荐
1 训练狗狗基础
2 怎么训练狗狗笑
3 怎么训练狗狗卧下
4 上海狗狗训练基地
5 训练狗狗排便
6 狗狗基础训练
8 如何训练狗狗装死
9 狗狗站姿训练
10 狗狗训练用品
11 训练狗狗吧
12 狗狗要怎么训练
13 怎么训练狗狗玩球
14 训练狗狗唤回
15 狗狗训练躺下
16 狗狗训练指令
17 狗狗的训练技巧
18 狗狗不好训练