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 }

打印结果:

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

最新文章

  1. The 11 advantages of Java -Why you choose this language
  2. ListView适配器获取布局文件作为View的三种方式
  3. 【MongoDB】mongoimport and mongoexport of data (一)
  4. ssl/https双向验证的配置
  5. ajax-典型应用-验证用户名
  6. 【转】IT职场人生系列之四:怎样写简历
  7. 写自己的WPF样式 - 窗体
  8. Java---设计模块(单例的变形)(多例)
  9. 第2章 开始入手 —— 01 创建第一个 Android 应用程序
  10. 关于_cmd
  11. iOS9.3越狱
  12. [转载] Hive结构
  13. hihocoder 1054 滑动解锁 dfs
  14. 程序包org.junit不存在
  15. PHP:自己写的mysql操作类
  16. h5 调起ios数字键盘的坑,限制特殊字符输入方案
  17. vue-cli 部分浏览器不支持es6的语法-babel-polyfill的引用和使用
  18. unity中将多个图片进行椭圆排序
  19. 制作Visual Studio 2019 (VS 2019) 离线安装包
  20. [20180705]关于hash join 2.txt

热门文章

  1. CephFS cache tier实践
  2. zabbix实现自定义自动发现的流程
  3. ThreadLocal应用及源码分析
  4. deepin 安装最新版node
  5. Maven项目关系
  6. Redis安全学习
  7. Nginx 解析漏洞复现
  8. Hadoop分布式平台搭建
  9. Improving Commonsense Question Answering by Graph-based Iterative Retrieval over Multiple Knowledge Sources —— 基于多知识库迭代检索的常识问答系统
  10. iOS中跑马灯效果小结