STL——容器(deque)deque 的插入 insert()
2024-08-28 04:26:53
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 }
打印结果:
===================================================================================================================
最新文章
- textField设置输入文字距左边的距离
- NOIp 2012 #2 借教室 Label:区间修改线段树
- Xcode 创建静态库和动态库
- WPF中使用ValueConverter来实现“范围条件触发器”
- ubuntu安装kernel3.10.34
- SwapEffect 枚举(定义交换效果)
- Spring框架整合Struts2
- Stooge排序
- HTTP发送请求模拟
- html常见标签使用
- 在ubuntu中为navicat创建快捷方式
- .11-Vue源码之patch(1)
- maven导入外部jar包的方法
- css Margin-top塌陷,解决方法
- 吴裕雄 python 机器学习——线性回归模型
- jQuery使用(六):DOM操作之元素包裹、克隆DOM与data的综合应用
- CF 494B 【Obsessive String】
- hdoj:2031
- a标签总结
- Win2012&;Win2008双系统启动菜单设置