vector::push_back和vector::emplace_back区别

emplace_back() 和 push_back() 功能上类似,但底层实现机制是不同的。push_back() 向容器尾部添加元素时,首先会创建这个元素(调用构造函数),然后再将这个元素拷贝或者移动到容器中(调用复制构造函数或者移动构造函数);而 emplace_back() 在实现时,则是直接在容器尾部创建这个元素,也就是只会调用一次构造函数。

简单实例

下面给出一个简单的例子:

#include <vector>
#include <iostream>
using namespace std;
class testDemo
{
public:testDemo(int num):num(num){std::cout << "call constructor function" << endl;}testDemo(const testDemo& other) :num(other.num) {std::cout << "call copy constructor function" << endl;}testDemo(testDemo&& other) :num(other.num) {std::cout << "call move constructor function" << endl;}
private:int num;
};int main()
{cout << "emplace_back:" << endl;std::vector<testDemo> demo1;demo1.emplace_back(2);cout << "push_back:" << endl;std::vector<testDemo> demo2;demo2.push_back(2);
}

输出结果如下:

emplace_back:
call constructor function
push_back:
call constructor function
call move constructor function

可以看到push_back会调用构造函数和移动构造函数,而emplace_back只会调用构造函数,需要注意push_back在类有移动构造函数的时候会默认先调用移动构造函数,若没有移动构造函数才会调用复制构造函数,例如如果把移动构造函数去掉:

#include <vector>
#include <iostream>
using namespace std;
class testDemo
{
public:testDemo(int num):num(num){std::cout << "call constructor function" << endl;}testDemo(const testDemo& other) :num(other.num) {std::cout << "call copy constructor function" << endl;}/*testDemo(testDemo&& other) :num(other.num) {std::cout << "call move constructor function" << endl;}*/
private:int num;
};int main()
{cout << "emplace_back:" << endl;std::vector<testDemo> demo1;demo1.emplace_back(2);cout << "push_back:" << endl;std::vector<testDemo> demo2;demo2.push_back(2);
}

输出结果如下:

emplace_back:
call constructor function
push_back:
call constructor function
call copy constructor function

一个更复杂实例

下面给一个更复杂的实例:

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <vector>
using namespace std;class Str{public:char *str;int id;Str(const char value[], const int id){cout<<"origin construct function..."<< value <<endl;str = NULL;int len = strlen(value);str = new char[len + 1];memset(str,0,len + 1);strcpy(str,value);this->id = id;}Str(const Str &s){cout<<"copy construct function..." << s.str<<endl;str = NULL;int len = strlen(s.str);str = new char[len + 1];memset(str,0,len + 1);strcpy(str,s.str);this->id = s.id;}Str(Str &&s){cout<<"move construct function..."<<endl;str = s.str;id = s.id;s.str = NULL;}~Str(){if(str != NULL){cout<<"destruct funciton..." <<str<<endl;delete []str;str = NULL;}}
};
int main()
{vector<Str> strVec;vector<Str> strVec1;cout<<"********push_back****************"<<endl;strVec.push_back(Str("abc",123));cout<<"********emplace_back****************"<<endl;strVec1.emplace_back("def",345);cout<<"********finish****************"<<endl;return 0;
}

输出结果如下:

********push_back****************
origin construct function...abc
move construct function...
********emplace_back****************
origin construct function...def
********finish****************
destruct funciton...def
destruct funciton...abc

可以看到,emplace_back支持直接传入构造函数的参数来构造创建元素,而push_back必须先构造元素,再通过复制构造函数传入。

C++11介绍之vector::push_back和vector::emplace_back区别相关推荐

  1. matlab vector用法,C++ vector 用法汇总

    标准库Vector类型 使用需要的头文件: #include Vector:Vector 是一个类模板.不是一种数据类型. Vector是一种数据类型. 一.  定义和初始化 Vectorv1;    ...

  2. c 语言vector用法,C++ Vector用法深入剖析

    C++编程语言中有一种叫做Vector的应用方法,它的作用在实际编程中是非常重要的.在这里我们将会为大家详细介绍一下C++ Vector的相关应用技巧及基本内容,希望能给大家带来一些帮助. (1)ve ...

  3. 一步步带你观察vector.push_back()具体拷贝机制,超级详细哦

    目录 观察vector.push_back()具体拷贝机制 实验需要条件 实验代码: 实验结果: 分析实验结果 步骤1 步骤2 步骤3 步骤4 步骤5 步骤6 步骤7 步骤8&9 步骤10 实 ...

  4. vector push_back()值传递到底啥意思?

    The content of val is copied (or moved) to the new element. 按照文档上的说明: 如果传递的对象,push_back就是对象的副本,push_ ...

  5. c++中list、vector、map 、set区别

    List封装了链表,Vector封装了数组, list和vector得最主要的区别在于vector使用连续内存存储的,他支持[]运算符,而list是以链表形式实现的,不支持[]. Vector对于随机 ...

  6. [算法] - c++ list, vector, map, set ,hashmap, deque区别(一)vector

    list不能随机访问,但vector可以,vector不能删除头部,而且插入删除的效率低. List封装了链表,Vector封装了数组, list和vector得最主要的区别在于vector使用连续内 ...

  7. c++ vector查找_C++ vector内存分配策略浅析

    (给CPP开发者加星标,提升C/C++技能) 来源:邱国禄https://blog.csdn.net/qiuguolu1108/article/details/107146184 vector是一个封 ...

  8. c++ vector 一部分_C++ vector 使用注意事项

    作者:Leehm 链接:https://www.cnblogs.com/leehm/p/10929756.html 1.初始化 c++ 11以后新增了大括号{}的初始化方式,需要注意与()的区别,如: ...

  9. 什么是vector C++学习vector浅析

    vector浅析 一.什么是vector? 二.容器特性 1.顺序序列 2.动态数组 3.动态处理内存需求 三.基本函数实现 1.构造函数 2.增加函数 4.遍历函数 5.判断函数 6.大小函数 7. ...

最新文章

  1. 王喜文:图解新基建,细说新机遇(100图)
  2. 规范-编码规范总结(微信分销系统)
  3. 【爬虫笔记】Scrapy爬虫技术文章网站
  4. 搭建K8s集群(二进制方式)-操作系统初始化
  5. c语言atm实训报告总体框架图,实训报告-ATM.doc
  6. 从今天开始 好好规划自己
  7. .desktop 桌面快捷_Ubuntu 桌面手动添加应用快捷方式
  8. Zookeeper C API 指南一(准备工作)
  9. Fiddler配置https
  10. Tabular Editor学习笔记_3:警告标志及解决办法
  11. 网件刷breed_斐讯K2刷不死breed加padavan华硕固件教程
  12. 软件系统上线前演示剧本
  13. 除了深度学习,你需要知道AI技术的23个方向
  14. 北方经贸杂志北方经贸杂志社北方经贸编辑部2022年第10期目录
  15. 【一键激活win8.1系统】
  16. word||标题序号和标题内容间隔很大
  17. Ubuntu由于没有公钥,无法验证下列签名。
  18. 微信世界争霸服务器,微信世界争霸小程序进不去游戏怎么办
  19. iceberg-flink 十:flink 窗口,事件时间,处理时间。
  20. 10分钟读懂什么是产品定位

热门文章

  1. UOJ #11.【UTR #1】ydc的大树 题解
  2. 惠普omen测试软件,性能测试:高品质体验主流游戏
  3. 解决旧笔记本电脑的ME固件的驱动安装程序不支持WIN10安装的方法
  4. 数据结构java朱战立pdf_数据结构使用C语言 朱战立,刘天时编著.pdf
  5. keil工程 freertos AC5编译器移植到AC6编译器
  6. pdf怎么删除其中一页与添加新页面
  7. nvme固态硬盘开机慢_解决nvme固态开机慢
  8. ARM SCP-firmware 代码解析
  9. 阿里邮箱(@aliyun.com):启用IMAP功能+邮箱密码登录
  10. DSP学习(5)—— Timer的使用