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

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

#include

#include

using namespace std;

int main()

{

std::vector demo{1,2};

//第一种格式用法

demo.insert(demo.begin() + 1, 3);//{1,3,2}

//第二种格式用法

demo.insert(demo.end(), 2, 5);//{1,3,2,5,5}

//第三种格式用法

std::arraytest{ 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

#include

using namespace std;

int main()

{

std::vector 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

#include

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 demo2{};

demo2.insert(demo2.begin(), testDemo(1));

cout << "emplace:" << endl;

std::vector demo1{};

demo1.emplace(demo1.begin(), 1);

return 0;

}

运行结果为:

insert:

调用构造函数

调用移动构造函数

emplace:

调用构造函数

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

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值