C++ STL 中的 deque emplace_front() 和 deque emplace_back()
本文的任务是展示 C++ STL 中 deque emplace_front() 和 deque emplace_back() 函数的功能。
什么是 Deque
Deque 是双端队列,是一种序列容器,可以在两端进行扩展和收缩操作。队列数据结构只允许用户在尾部插入数据,并在头部删除数据。让我们以公交车站的队伍为例,乘客只能从队伍的尾部加入,而站在队伍头部的人是第一个被移除的人,而在双端队列中,可以在两端插入和删除数据。
什么是 emplace_front() 函数
emplace_front() 函数在 deque 的开头插入新元素。
语法
dequename.emplace_front(value)
参数
value - 定义要插入到 deque 开头的新的元素
示例
**输入** Deque - 12 13 14 15 16
**输出** 新的 Deque - 11 12 13 14 15 16
**输入** Deque - O R C E
**输出** 新的 Deque: F O R C E
可以遵循的方法
首先,我们声明 deque。
然后,我们打印 deque。
然后,我们定义 emplace_front() 函数。
然后,我们在插入新元素后打印新的 deque。
使用上述方法,我们可以在开头输入新元素。在定义函数时,我们将新元素定义为参数。新元素应该与 deque 具有相同的数据类型。
示例
// C++ code to demonstrate the working of deque emplace_front( ) function
#include<iostream.h>
#include<deque.h>
Using namespace std;
int main ( ){
// initializing the deque
Deque<int> deque = { 85, 87, 88, 89, 90 };
// print the deque
cout<< “ Deque: “;
for( auto x = deque.begin( ); x != deque.end( ); ++x)
cout<< *x << “ “;
// defining the emplace_front( ) function
deque.emplace_front(78);
// printing deque after inserting new element
cout<< “ New Deque:”;
for( x = deque.begin( ) ; x != deque.end( ); ++x)
cout<< “ “ <<*x;
return 0;
}输出
如果我们运行以上代码,它将生成以下输出
Input - Deque: 85 87 88 89 90 Output - New Deque: 78 85 87 88 89 90 Input – Deque: O I S E Output – New Deque: N O I S E
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP