STL中C ++中的deque_insert()
给出的任务是显示C++STL中Dequeinsert()函数的功能
什么是双端队列?
双端队列是双端队列,它是序列容器,在两端都提供扩展和收缩功能。队列数据结构允许用户仅在END插入数据,并从FRONT删除数据。让我们以在公交车站排队的类比为例,那里的人只能从END插入队列,而站在FRONT的人是第一个被移走的人,而在双头队列中,可以同时插入和删除数据结束。
什么是insert()
双端队列insert()函数用于将元素插入双端队列。
该函数用于在指定位置插入元素。
该函数还用于在双端队列中插入元素的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)
参数
值–指定要插入的新元素。
n–指定要插入的元素数。
first,last–指定迭代器,该迭代器指定要插入的元素范围。
返回值
它返回指向新插入元素的第一个的迭代器。
示例
输入双端队列−12345
输出新双端队列-112345
输入双端队列-1112131415
输出新双端输出-1112121212131415
可以遵循的方法
首先我们声明双端队列。
然后我们打印双端队列。
然后我们声明insert()函数。
通过使用上述方法,我们可以插入新元素。
示例
// C++ code to demonstrate the working of deque insert( ) function
#include<iostream.h>
#include<deque.h>
Using namespace std;
int main ( ){
//宣布双端队列
Deque<int> deque = { 55, 84, 38, 66, 67 };
//打印双端队列
cout<< “ Deque: “;
for( auto x = deque.begin( ); x != deque.end( ); ++x)
cout<< *x << “ “;
//声明insert()函数
x = deque.insert(x, 22);
//插入新元素后打印双端队列
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’);
//插入新元素后打印双端队列
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( ) );
//插入新元素后打印双端队列
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