STL—— 容器(vector)的数据插入之 insert()
2024-09-07 14:10:12
vector 容器可以使用 vectorName.insert() 方法插入元素,vectorName.insert() 函数一共有4种重载方法:
第一种 insert() 用法:在指定地址插入单个元素
1 #include <iostream>
2 #include <vector>
3
4 using namespace std;
5
6 int main()
7 {
8 int test[] = { 111,222,333,444 };
9 vector<int>num(test, test + 4);
10
11 cout << "==========遍历容器===========" << endl;
12
13 for (int i = 0; i < num.size(); i++)
14 {
15 cout << num.at(i) << endl;
16 }
17
18 num.insert(num.begin(), 888); //初始位置插入 888 ,第一个参数需使用迭代器来确认插入位置
19
20 cout << "===使用 insert() 后遍历容器===" << endl;
21
22 for (int i = 0; i < num.size(); i++)
23 {
24 cout << num.at(i) << endl;
25 }
26
27 return 0;
28 }
打印结果:
由于第一个参数是迭代器,由于是地址,我们也可以对其进行地址加减操作进行元素插入,比如在第二个元素后插入元素,我们用 insert() 可以这样写:
1 #include <iostream>
2 #include <vector>
3
4 using namespace std;
5
6 int main()
7 {
8 int test[] = { 111,222,333,444 };
9 vector<int>num(test, test + 4);
10
11 cout << "==========遍历容器===========" << endl;
12
13 for (int i = 0; i < num.size(); i++)
14 {
15 cout << num.at(i) << endl;
16 }
17
18 num.insert(num.begin() + 2, 888); //初始位置插入 888 ,第一个参数需使用迭代器来确认插入位置
19
20 cout << "===使用 insert() 后遍历容器===" << endl;
21
22 for (int i = 0; i < num.size(); i++)
23 {
24 cout << num.at(i) << endl;
25 }
26
27 return 0;
28 }
打印结果:
第二种 insert() 用法:在指定地址插入多个元素
最普通的多个元素插入:
1 #include <iostream>
2 #include <vector>
3
4 using namespace std;
5
6 int main()
7 {
8 int test[] = { 111,222,333,444 };
9 vector<int>num(test, test + 4);
10
11 cout << "==========遍历容器===========" << endl;
12
13 for (int i = 0; i < num.size(); i++)
14 {
15 cout << num.at(i) << endl;
16 }
17
18 num.insert(num.begin(), 3, 888); //初始位置插入3个 888 ,第一个参数需使用迭代器来确认插入位置
19
20 cout << "===使用 insert() 后遍历容器===" << endl;
21
22 for (int i = 0; i < num.size(); i++)
23 {
24 cout << num.at(i) << endl;
25 }
26
27 return 0;
28 }
打印结果:
与第一种用法一样,由于第一个参数是迭代器地址,可以通过地址加减控制插入位置。
第三种 insert() 用法:在指定地址插入其他容器的迭代器
我们也可以将其他同类型容器的元素插入到当前容器,如下代码使用三个迭代器来插入多个元素:
1 #include <iostream>
2 #include <vector>
3
4 using namespace std;
5
6 int main()
7 {
8 int test[] = { 111,222,333,444 };
9 vector<int>num(test, test + 4);
10 vector<int>num_1(test + 2, test + 4);
11
12 cout << "==========遍历容器===========" << endl;
13
14 for (int i = 0; i < num.size(); i++)
15 {
16 cout << num.at(i) << endl;
17 }
18
19 num.insert(num.begin(), num_1.begin(), num_1.end()); //初始位置插入 从 begin 到 end 的元素
20
21 cout << "===使用 insert() 后遍历容器===" << endl;
22
23 for (int i = 0; i < num.size(); i++)
24 {
25 cout << num.at(i) << endl;
26 }
27
28 return 0;
29 }
打印结果:
===========================================================================================================================
最新文章
- The 11 advantages of Java -Why you choose this language
- ListView适配器获取布局文件作为View的三种方式
- 【MongoDB】mongoimport and mongoexport of data (一)
- ssl/https双向验证的配置
- ajax-典型应用-验证用户名
- 【转】IT职场人生系列之四:怎样写简历
- 写自己的WPF样式 - 窗体
- Java---设计模块(单例的变形)(多例)
- 第2章 开始入手 —— 01 创建第一个 Android 应用程序
- 关于_cmd
- iOS9.3越狱
- [转载] Hive结构
- hihocoder 1054 滑动解锁 dfs
- 程序包org.junit不存在
- PHP:自己写的mysql操作类
- h5 调起ios数字键盘的坑,限制特殊字符输入方案
- vue-cli 部分浏览器不支持es6的语法-babel-polyfill的引用和使用
- unity中将多个图片进行椭圆排序
- 制作Visual Studio 2019 (VS 2019) 离线安装包
- [20180705]关于hash join 2.txt