STL——容器(deque)deque 的插入 insert()
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()的更多相关文章
- STL——容器(deque) 构造 & 头尾添加删除元素
1.deque容器概念 deque是"double-ended queue"的缩写,和vector一样都是STL的容器,唯一不同的是:deque是双端数组,而vector是单端的. ...
- STL——容器(Set & multiset) insert 的返回值 和 pair 的用法
1. 使用 insert 插入时的返回值: 将一个元素插入 (insert) 到 set 或 multiset 中时,如果插入失败返回的类型是一个 pair 的自定类型,insert 源码如下: in ...
- STL容器:deque双端队列学习
所谓deque,是"double-ended queue"的缩写; 它是一种动态数组形式,可以向两端发展,在尾部和头部插入元素非常迅速; 在中间插入元素比较费时,因为需要移动其它元 ...
- STL容器分析--deque
deque,故名思义,双向队列.可以在头尾进行插入删除. 而STL中采用了链表+线性表的数据结构来实现deque,因而除了满足双向队列的特点以外,还支持随机访问. 下面,贴一段代码. 总览:双向队列是 ...
- STL——容器(deque)deque 的删除 clear() erase()
deque.clear(); //移除容器的所有数据 1 #include <iostream> 2 #include <deque> 3 4 using namespace ...
- STL——容器(deque) deque 的大小
1. deque 的大小 deque.size(); //返回容器中元素的个数 1 #include <iostream> 2 #include <dequ ...
- STL——容器(deque) 元素的存取&迭代器
1. deque 的数据存取 这个部分和 vector 几乎一样 第一 使用下标操作 dequeName[0] = 100; //小心越界 第二 使用at 方法 如: dequeName.at(2 ...
- STL——容器(deque) deque 的赋值 assign() operator=() swap()
deque 的赋值分下边4种方法: deque.assign(beg,end); //将[beg, end)区间中的数据拷贝赋值给本身.注意该区间是左闭右开的区间. 1 #include <io ...
- STL容器存储的内容动态分配情况下的内存管理
主要分两种情况:存储的内容是指针:存储的内容是实际对象. 看以下两段代码, typedef pair<VirObjTYPE, std::list<CheckID>*> VirO ...
随机推荐
- 深度解读最新版 Scrum 指南
本文作者:CODING - 敏杰小王子 11 月 18 日晚,Scrum 框架的创始人 Jeff Sutherland 和 Ken Schwaber 联手发布了最新版 Scrum 指南.作为 Scru ...
- Centos快速安装Docke
预备 删除旧docker # 删除旧docker $ sudo yum remove docker \ docker-client \ docker-client-latest \ docker-co ...
- Go语言实现excel导入无限级菜单结构
目录 需求 实现 测试 简单例子 复杂例子 需求 最近有一个需求,要实现一个无限级结构的树型菜单,差不多长下面这个样子 我们知道无限级实现思路都是用一个parent_id将各种层级串联起来,顶级的pa ...
- Python3 Socket
Socket socket介绍 socket意为套接字,是应用层与传输层TCP/IP,UDP之间通信的中间软件抽象层,它是一组接口.使用时只需遵循socket的格式与规定去编程,让socket组织数据 ...
- idea中安装阿里巴巴的代码规范插件
1.打开iead软件,从左上角点击File -> Settings -> Plugins 2.安装完成后,重启idea软件,即可正常使用了.
- C Looooops POJ - 2115
数论好题.. 香! 首先我们看到这一题, 题意是 \[a + c * x \equiv b (mod \ \ 2 ^ k) \] 对此式移一下项, 得 \[c * x \equiv b - a (mo ...
- Java基础教程——运算符
运算符 算术运算符 加 减 乘 除 取余 自加 自减 + - * / % ++ -- public class 算术运算符 { public static void main(String[] arg ...
- 顺序结构(C语言基本结构)
顺序结构 1.基本概念 语句执行的顺序与顺序程序书写的顺序一致 特点 a.程序执行的顺序和语句书写的顺序一致 b.有一个数据入口,一个数据出口 顺序结构与四则运算 顺序结构是C语言的基本结构 程序由上 ...
- LeetCode 042 Trapping Rain Water
题目要求:Trapping Rain Water Given n non-negative integers representing an elevation map where the width ...
- EntityFramework Core健康检查
前言 .NET Core提供对应方法可进行健康检查,那么在EF Core中是否也提供了相应的方式呢?EF Core 2.2+(包含2.2)版本提供了针对上下文的健康检查,接下来我们直接利用.NET 5 ...