deque.insert(pos,elem);

//在pos位置插入一个elem元素的拷贝,返回新数据的位置。

 1 #include <iostream>
2 #include <deque>
3
4 using namespace std;
5
6 int main()
7 {
8 deque<int> deqInt_A, deqInt_B;
9
10 deqInt_A.push_back(1);
11 deqInt_A.push_back(2);
12 deqInt_A.push_back(3);
13
14 cout << "遍历deqInt_A" << endl;
15 for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end(); it++)
16 {
17 cout << *it << " ";
18 }
19
20 //在首位置插入1个数
21 deqInt_A.insert(deqInt_A.begin(), 0);
22
23 cout << "\ninsert 插入后,遍历deqInt_A" << endl;
24 for (deque<int>::iterator it = deqInt_A.begin(); it!= deqInt_A.end(); it++)
25 {
26 cout << *it << " ";
27 }
28
29 return 0;
30 }

打印结果:

deque.insert(pos,n,elem);

//在pos位置插入n个elem数据,无返回值。

 1 #include <iostream>
2 #include <deque>
3
4 using namespace std;
5
6 int main()
7 {
8 deque<int> deqInt_A, deqInt_B;
9
10 deqInt_A.push_back(1);
11 deqInt_A.push_back(2);
12 deqInt_A.push_back(3);
13
14 cout << "遍历deqInt_A" << endl;
15 for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end(); it++)
16 {
17 cout << *it << " ";
18 }
19
20 //在首位置+1处 插入2个0
21 deqInt_A.insert(deqInt_A.begin() + 1, 2, 0);
22
23 cout << "\ninsert 插入后,遍历deqInt_A" << endl;
24 for (deque<int>::iterator it = deqInt_A.begin(); it!= deqInt_A.end(); it++)
25 {
26 cout << *it << " ";
27 }
28
29 return 0;
30 }

打印结果:

deque.insert(pos,beg,end);

//在pos位置插入[beg,end)区间的数据,无返回值

 1 #include <iostream>
2 #include <deque>
3
4 using namespace std;
5
6 int main()
7 {
8 deque<int> deqInt_A, deqInt_B;
9
10 deqInt_A.push_back(1);
11 deqInt_A.push_back(2);
12 deqInt_A.push_back(3);
13
14 deqInt_B.push_back(10);
15 deqInt_B.push_back(20);
16 deqInt_B.push_back(30);
17
18 cout << "遍历deqInt_A" << endl;
19 for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end(); it++)
20 {
21 cout << *it << " ";
22 }
23 cout << "\n遍历deqInt_B" << endl;
24 for (deque<int>::iterator it = deqInt_B.begin(); it != deqInt_B.end(); it++)
25 {
26 cout << *it << " ";
27 }
28
29 //在首位置+1处 插入deqInt_B
30 deqInt_A.insert(deqInt_A.begin() + 1, deqInt_B.begin(), deqInt_B.end());
31 cout << "\n在首位置+1处 插入deqInt_B" << endl;
32 for (deque<int>::iterator it = deqInt_A.begin(); it!= deqInt_A.end(); it++)
33 {
34 cout << *it << " ";
35 }
36
37 return 0;
38 }

打印结果:

===================================================================================================================

STL——容器(deque)deque 的插入 insert()的更多相关文章

  1. STL——容器(deque) 构造 & 头尾添加删除元素

    1.deque容器概念 deque是"double-ended queue"的缩写,和vector一样都是STL的容器,唯一不同的是:deque是双端数组,而vector是单端的. ...

  2. STL——容器(Set & multiset) insert 的返回值 和 pair 的用法

    1. 使用 insert 插入时的返回值: 将一个元素插入 (insert) 到 set 或 multiset 中时,如果插入失败返回的类型是一个 pair 的自定类型,insert 源码如下: in ...

  3. STL容器:deque双端队列学习

    所谓deque,是"double-ended queue"的缩写; 它是一种动态数组形式,可以向两端发展,在尾部和头部插入元素非常迅速; 在中间插入元素比较费时,因为需要移动其它元 ...

  4. STL容器分析--deque

    deque,故名思义,双向队列.可以在头尾进行插入删除. 而STL中采用了链表+线性表的数据结构来实现deque,因而除了满足双向队列的特点以外,还支持随机访问. 下面,贴一段代码. 总览:双向队列是 ...

  5. STL——容器(deque)deque 的删除 clear() erase()

    deque.clear(); //移除容器的所有数据 1 #include <iostream> 2 #include <deque> 3 4 using namespace ...

  6. STL——容器(deque) deque 的大小

    1. deque 的大小 deque.size();              //返回容器中元素的个数 1 #include <iostream> 2 #include <dequ ...

  7. STL——容器(deque) 元素的存取&迭代器

    1. deque 的数据存取 这个部分和 vector 几乎一样 第一  使用下标操作 dequeName[0] = 100; //小心越界 第二  使用at 方法 如: dequeName.at(2 ...

  8. STL——容器(deque) deque 的赋值 assign() operator=() swap()

    deque 的赋值分下边4种方法: deque.assign(beg,end); //将[beg, end)区间中的数据拷贝赋值给本身.注意该区间是左闭右开的区间. 1 #include <io ...

  9. STL容器存储的内容动态分配情况下的内存管理

    主要分两种情况:存储的内容是指针:存储的内容是实际对象. 看以下两段代码, typedef pair<VirObjTYPE, std::list<CheckID>*> VirO ...

随机推荐

  1. RBD快速删除的方法分析与改进

    前言 这个问题在很久以前就有一篇文章进行过讨论 remove-big-rbd,这个文章写的比较清楚了,并且对不同的方法做了分析,这里先把结论说下 rbd类型 rbd rm 方法 rados -p rm ...

  2. 转载 数据库优化 - SQL优化

    判断问题SQL判断SQL是否有问题时可以通过两个表象进行判断: 系统级别表象CPU消耗严重IO等待严重页面响应时间过长应用的日志出现超时等错误可以使用sar命令,top命令查看当前系统状态. 也可以通 ...

  3. [PHP安全特性学习]strcmp()函数安全漏洞

    简介 PHP函数的安全特性-strcmp() 函数 php-strcmp()函数 PHP strcmp() 函数 strcmp() 函数比较两个字符串. 注释:strcmp() 函数是二进制安全的,且 ...

  4. 微软开源的 AI 工具,让旧照片焕发新生

    原文地址:Bringing Old Photos Back to Life 原文作者:Ziyu Wan 译者 & 校正:HelloGitHub-小鱼干 & 鸭鸭 写在前面 在 GitH ...

  5. 【MathType教学】如何让括号内的内容居中

    作为一款非常好用的公式编辑器,MathType它的功能十分强大,不仅包含了大量的数学符号,并且能和Office软件很好地兼容.但是有的时候打出来的公式可能不是自己想要的效果,这时我们就要用一些特别的办 ...

  6. C# redis集群批量操作之slot计算出16384个字符串

    引入一个大家都用的到的需求来说吧. 需求:要在三主三从的redis集群,存入数据,会对数据进行批量删除操作,数据要求要在redis集群负载均衡. 思路: 1.存入数据好办 1 var connect ...

  7. JUC并发工具包之CyclicBarrier & CountDownLatch的异同

    1.介绍 本文我们将比较一下CyclicBarrier和CountDownLatch并了解两者的相似与不同. 2.两者是什么 当谈到并发,将这两者概念化的去解释两者是做什么的,这其实是一件很有挑战的事 ...

  8. mysql 分组查询

    mysql 分组查询 获取id最大的一条 (1)分组查询获取最大id SELECT MAX(id) as maxId FROM `d_table` GROUP BY `parent_id` ; (2) ...

  9. Running Median POJ - 3784

    本题使用对顶堆做法. 为了动态维护中位数,我们可以建立两个堆 :一个大根对,一个小根堆. 用法:在动态维护的过程中,设当前的长度为length,大根堆存从小到大排名 $1 \thicksim \dfr ...

  10. Java集合【3】-- iterable接口超级详细解析

    目录 iterable接口 1. 内部定义的方法 1.1 iterator()方法 1.2 forEach()方法 1.3 spliterator()方法 总结 iterable接口 整个接口框架关系 ...