C++ STL 中的 deque front() 和 deque back()
本文旨在展示 C++ STL 中 deque front() 和 deque back() 函数的功能
什么是双端队列
双端队列是双端队列,也就是一种序列容器,可提供两端的扩展和收缩功能。队列数据结构允许用户仅从 END 插入数据,并从 FRONT 删除数据。让我们以公交车站的队列为例,人员只能从 END 排队,而排在 FRONT 的人则是第一个被带走的人,而在双端队列中,数据可以在两端插入和删除。
什么是 deque front() 函数
front() 函数用于引用双端队列的第一个元素。
语法
dequename.front( )
示例
输入双端队列:12 13 14 15 16
输出新双端队列:12
输入双端队列:C A P T U R E
输出新双端队列:C
可遵循的方法
首先声明双端队列
然后打印该双端队列。
然后定义 front() 函数。
通过使用如上方法,我们可以获取双端队列的第一个元素。
示例
// C++ code to demonstrate the working of deque front( ) function
#include<iostream.h>
#include<deque.h>
Using namespace std;
int main ( ){
// initializing the deque
Deque<int> deque = { 5, 7, 6, 8, 9 };
// print the deque
cout<< “ Deque: “;
for( auto x = deque.begin( ); x != deque.end( ); ++x)
cout<< *x << “ “;
// defining the front( ) function
cout<< deque.front( );
return 0;
}输出
如果我们运行以上代码,它将生成以下输出
Input – Deque: 5 7 6 8 9 Output – New Deque: 5 Input – Deque: L O N D O N Output – New Deque: L
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP