C++ STL 中的 deque_insert()
本任务旨在展示 C++ STL 中 Deque insert() 函数的功能。
什么是 Deque?
Deque 是双端队列,是一种序列容器,可以在两端进行扩展和收缩操作。队列数据结构只允许用户在队尾插入数据,在队首删除数据。打个比方,就像公交车站的队伍,只能在队尾排队,而排在队首的人先下车。但在双端队列中,可以在两端进行数据的插入和删除。
什么是 insert()?
deque insert() 函数用于在 deque 中插入元素。
该函数用于在指定位置插入元素。
该函数还用于在 deque 中插入 n 个元素。
它还用于在指定位置插入指定范围内的元素。
语法
deque_name.insert (iterator position, const_value_type& value) deque_name.insert (iterator position, size_type n, const_value_type& value) deque_name.insert (iterator position, iterator first, iterator last)
参数
Value – 指定要插入的新元素。
n – 指定要插入的元素数量。
first, last – 指定迭代器,用于指定要插入的元素范围。
返回值
它返回一个指向第一个新插入元素的迭代器。
示例
输入 Deque − 1 2 3 4 5
输出 新的 Deque − 1 1 2 3 4 5
输入 Deque − 11 12 13 14 15
输出 新的 Deque − 11 12 12 12 13 14 15
可遵循的方法
首先声明 deque。
然后打印 deque。
然后声明 insert() 函数。
使用上述方法,可以插入新元素。
示例
// C++ code to demonstrate the working of deque insert( ) function
#include<iostream.h>
#include<deque.h>
Using namespace std;
int main ( ){
// declaring the deque
Deque<int> deque = { 55, 84, 38, 66, 67 };
// print the deque
cout<< “ Deque: “;
for( auto x = deque.begin( ); x != deque.end( ); ++x)
cout<< *x << “ “;
// declaring insert( ) function
x = deque.insert(x, 22);
// printing deque after inserting new element
cout<< “ New Deque:”;
for( x = deque.begin( ); x != deque.end( ); ++x)
cout<< “ “ <<*x;
return 0;
}输出
如果运行上述代码,则会生成以下输出
Input - Deque: 55 84 38 66 67 Output - New Deque: 22 55 84 38 66 67
示例
// C++ code to demonstrate the working of deque insert( ) function
#include<iostream.h>
#include<deque.h>
Using namespace std;
int main( ){
deque<char> deque ={ ‘B’ , ‘L’ , ‘D’ };
cout<< “ Deque: “;
for( auto x = deque.begin( ); x != deque.end( ); ++x)
cout<< *x << “ “;
deque.insert(x + 1, 2, ‘O’);
// printing deque after inserting new element
cout<< “ New Deque:”;
for( auto x = deque.begin( ); x != deque.end( ); ++x)
cout<< “ “ <<*x;
return 0;
}输出
如果运行上述代码,则会生成以下输出
Input – Deque: B L D Output – New Deque: B L O O D
示例
// C++ code to demonstrate the working of deque insert( ) function
#include<iostream.h>
#include<deque.h>
#include<vector.h>
Using namespace std;
int main( ){
deque<int> deque ={ 65, 54, 32, 98, 55 };
cout<< “ Deque: “;
for( auto x = deque.begin( ); x != deque.end( ); ++x)
cout<< *x << “ “;
vector<int7gt; v(3, 19);
deque.insert(x, v.begin( ), v.end( ) );
// printing deque after inserting new element
cout<< “ New Deque:”;
for( auto x = deque.begin( ); x != deque.end( ); ++x)
cout<< “ “ <<*x;
return 0;
}输出
如果运行上述代码,则会生成以下输出
Input – Deque: 65 54 32 98 55 Output – New Deque: 65 19 19 19 65 54 32 98 55
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP