2025年3月16日 星期日 甲辰(龙)年 月十五 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > STL

C++ STL vector插入元素(insert()和emplace())详解

时间:03-07来源:作者:点击数:438

vector容器提供了 insert() 和 emplace() 这 2 个成员函数,用来实现在容器指定位置处插入元素,本节将对它们的用法做详细的讲解。

另外,如果想实现在 vector 容器尾部添加元素,可阅读《vector添加元素》一节。

insert()

insert() 函数的功能是在 vector 容器的指定位置插入一个或多个元素。该函数的语法格式有多种,如表 1 所示。 

表 1 insert() 成员函数语法格式
语法格式 用法说明
iterator insert(pos,elem) 在迭代器 pos 指定的位置之前插入一个新元素elem,并返回表示新插入元素位置的迭代器。
iterator insert(pos,n,elem) 在迭代器 pos 指定的位置之前插入 n 个元素 elem,并返回表示第一个新插入元素位置的迭代器。
iterator insert(pos,first,last)  在迭代器 pos 指定的位置之前,插入其他容器(不仅限于vector)中位于 [first,last) 区域的所有元素,并返回表示第一个新插入元素位置的迭代器。
iterator insert(pos,initlist) 在迭代器 pos 指定的位置之前,插入初始化列表(用大括号{}括起来的多个元素,中间有逗号隔开)中所有的元素,并返回表示第一个新插入元素位置的迭代器。

下面的例子,演示了如何使用 insert() 函数向 vector 容器中插入元素。

  • #include <iostream>
  • #include <vector>
  • #include <array>
  • using namespace std;
  • int main()
  • {
  • std::vector<int> demo{1,2};
  • //第一种格式用法
  • demo.insert(demo.begin() + 1, 3);//{1,3,2}
  • //第二种格式用法
  • demo.insert(demo.end(), 2, 5);//{1,3,2,5,5}
  • //第三种格式用法
  • std::array<int,3>test{ 7,8,9 };
  • demo.insert(demo.end(), test.begin(), test.end());//{1,3,2,5,5,7,8,9}
  • //第四种格式用法
  • demo.insert(demo.end(), { 10,11 });//{1,3,2,5,5,7,8,9,10,11}
  • for (int i = 0; i < demo.size(); i++) {
  • cout << demo[i] << " ";
  • }
  • return 0;
  • }

运行结果为:

1 3 2 5 5 7 8 9 10 11

emplace()

emplace() 是 C++ 11 标准新增加的成员函数,用于在 vector 容器指定位置之前插入一个新的元素。

再次强调,emplace() 每次只能插入一个元素,而不是多个。

该函数的语法格式如下:

iterator emplace (const_iterator pos, args...);

其中,pos 为指定插入位置的迭代器;args... 表示与新插入元素的构造函数相对应的多个参数;该函数会返回表示新插入元素位置的迭代器。

简单的理解 args...,即被插入元素的构造函数需要多少个参数,那么在 emplace() 的第一个参数的后面,就需要传入相应数量的参数。

举个例子:

  • #include <vector>
  • #include <iostream>
  • using namespace std;
  • int main()
  • {
  • std::vector<int> demo1{1,2};
  • //emplace() 每次只能插入一个 int 类型元素
  • demo1.emplace(demo1.begin(), 3);
  • for (int i = 0; i < demo1.size(); i++) {
  • cout << demo1[i] << " ";
  • }
  • return 0;
  • }

运行结果为:

3 1 2

既然 emplace() 和 insert() 都能完成向 vector 容器中插入新元素,那么谁的运行效率更高呢?答案是 emplace()。在说明原因之前,通过下面这段程序,就可以直观看出两者运行效率的差异:

  • #include <vector>
  • #include <iostream>
  • using namespace std;
  • class testDemo
  • {
  • public:
  • testDemo(int num) :num(num) {
  • std::cout << "调用构造函数" << endl;
  • }
  • testDemo(const testDemo& other) :num(other.num) {
  • std::cout << "调用拷贝构造函数" << endl;
  • }
  • testDemo(testDemo&& other) :num(other.num) {
  • std::cout << "调用移动构造函数" << endl;
  • }
  • testDemo& operator=(const testDemo& other);
  • private:
  • int num;
  • };
  • testDemo& testDemo::operator=(const testDemo& other) {
  • this->num = other.num;
  • return *this;
  • }
  • int main()
  • {
  • cout << "insert:" << endl;
  • std::vector<testDemo> demo2{};
  • demo2.insert(demo2.begin(), testDemo(1));
  • cout << "emplace:" << endl;
  • std::vector<testDemo> demo1{};
  • demo1.emplace(demo1.begin(), 1);
  • return 0;
  • }

运行结果为:

insert:
调用构造函数
调用移动构造函数
emplace:
调用构造函数

注意,当拷贝构造函数和移动构造函数同时存在时,insert() 会优先调用移动构造函数。

可以看到,通过 insert() 函数向 vector 容器中插入 testDemo 类对象,需要调用类的构造函数和移动构造函数(或拷贝构造函数);而通过 emplace() 函数实现同样的功能,只需要调用构造函数即可。

简单的理解,就是 emplace() 在插入元素时,是在容器的指定位置直接构造元素,而不是先单独生成,再将其复制(或移动)到容器中。因此,在实际使用中,推荐大家优先使用 emplace()。

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门