emplace_back() 和 push_back 的区别(转)
在引入右值引用,转移构造函数,转移复制运算符之前,通常使用push_back()向容器中加入一个右值元素(临时对象)的时候,首先会调用构造函数构造这个临时对象,然后需要调用拷贝构造函数将这个临时对象放入容器中。原来的临时变量释放。这样造成的问题是临时变量申请的资源就浪费。
引入了右值引用,转移构造函数(请看这里)后,push_back()右值时就会调用构造函数和转移构造函数。
在这上面有进一步优化的空间就是使用emplace_back
emplace_back
函数原型:
template <class... Args>
void emplace_back (Args&&... args);
在容器尾部添加一个元素,这个元素原地构造,不需要触发拷贝构造和转移构造。而且调用形式更加简洁,直接根据参数初始化临时对象的成员。
给出一个示例,这个示例很有用。
#include <vector>
#include <string>
#include <iostream> struct President
{
std::string name;
std::string country;
int year; President(std::string p_name, std::string p_country, int p_year)
: name(std::move(p_name)), country(std::move(p_country)), year(p_year)
{
std::cout << "I am being constructed.\n";
}
President(const President& other)
: name(std::move(other.name)), country(std::move(other.country)), year(other.year)
{
std::cout << "I am being copy constructed.\n";
}
President(President&& other)
: name(std::move(other.name)), country(std::move(other.country)), year(other.year)
{
std::cout << "I am being moved.\n";
}
President& operator=(const President& other);
}; int main()
{
std::vector<President> elections;
std::cout << "emplace_back:\n";
elections.emplace_back("Nelson Mandela", "South Africa", ); //没有类的创建 std::vector<President> reElections;
std::cout << "\npush_back:\n";
reElections.push_back(President("Franklin Delano Roosevelt", "the USA", )); std::cout << "\nContents:\n";
for (President const& president: elections) {
std::cout << president.name << " was elected president of "
<< president.country << " in " << president.year << ".\n";
}
for (President const& president: reElections) {
std::cout << president.name << " was re-elected president of "
<< president.country << " in " << president.year << ".\n";
} }
输出
emplace_back:
I am being constructed. push_back:
I am being constructed.
I am being moved. Contents:
Nelson Mandela was elected president of South Africa in .
转自:https://blog.csdn.net/xiaolewennofollow/article/details/52559364
emplace_back() 和 push_back 的区别(转)的更多相关文章
- 学习 emplace_back() 和 push_back 的区别 emplace_back效率高
在引入右值引用,转移构造函数,转移复制运算符之前,通常使用push_back()向容器中加入一个右值元素(临时对象)的时候,首先会调用构造函数构造这个临时对象,然后需要调用拷贝构造函数将这个临时对象放 ...
- emplace_back与push_back的区别
std::vector::emplace_back C++ Containers library std::vector template< class... Args &g ...
- 【C/C++开发】emplace_back() 和 push_back 的区别
在引入右值引用,转移构造函数,转移复制运算符之前,通常使用push_back()向容器中加入一个右值元素(临时对象)的时候,首先会调用构造函数构造这个临时对象,然后需要调用拷贝构造函数将这个临时对象放 ...
- emplace_back() 和 push_back 的区别
在引入右值引用,转移构造函数,转移复制运算符之前,通常使用push_back()向容器中加入一个右值元素(临时对象)的时候,首先会调用构造函数构造这个临时对象,然后需要调用拷贝构造函数将这个临时对象放 ...
- 编程杂谈——使用emplace_back取代push_back
近日在YouTube视频上看到关于vector中emplace_back与push_back区别的介绍,深感自己在现代C++中还是有不少遗漏的知识点,遂写了段代码,尝试比较两者的差别. 示例代码 #i ...
- emplace_back与push_back
资料参考: https://blog.csdn.net/p942005405/article/details/84764104 实际精华在评论中,转载如下: STL的实现版本很多,VS.GCC版本不同 ...
- C++11使用emplace_back代替push_back
最近在写一段代码的时候,突然很好奇C++11中对push_back有没有什么改进以增加效率,上网搜了一些资料,发现果然新增了emplace_back方法,比push_back的效率要高很多. 首先,写 ...
- C++11 vector使用emplace_back代替push_back
C++11中,针对顺序容器(如vector.deque.list),新标准引入了三个新成员:emplace_front.emplace和emplace_back,这些操作构造而不是拷贝元素.这些操作分 ...
- (转)C++11使用emplace_back代替push_back (其中有关于右值引用)
最近在写一段代码的时候,突然很好奇C++11中对push_back有没有什么改进以增加效率,上网搜了一些资料,发现果然新增了emplace_back方法,比push_back的效率要高很多. 首先,写 ...
随机推荐
- 升级IOS8游戏上传自定义头像功能失效的问题
为了支持arm64,之前已经折腾了很久,昨晚打包准备提交苹果审核时,测试那边的同事反馈说游戏上传自定义头像功能不可用了. 游戏上传自定义功能的简介:卡牌游戏最初是<比武招亲>中有一个充VI ...
- 关于android studio 出现Error:Execution failed for task ':app:preDebugAndroidTestBuild'. 的解决办法
Error:Execution failed for task ':app:preDebugAndroidTestBuild'. > Conflict with dependency 2018年 ...
- 低版本系统兼容的ActionBar(七)自定义Actionbar标题栏字体
这个自定义字体其实和ActionBar有关,但之前写AtionBar的时候没考虑到修改字体样式,今天看到一篇专门写这个的文章就贴上使用方式.╮(╯▽╰)╭,不得不说Actionbar的那个样式真是让人 ...
- Java Type Inference (类型推断)
public class Test2 { public static void main(String[] args) { ArrayList<String> list = newArra ...
- arcgis server 10 for java 8399根目录是404的提示取消,并跳转到 地图目录 /arcgis/rest/services下
看了Howto: 取消ArcGIS Server 9.x for Java内置tomcat在8399端口的文件列表 http://support.esrichina-bj.cn/2009/0819/9 ...
- Oracle简单的备份和恢复-导入和导出-目录
ylbtech-Oracle:Oracle简单的备份和恢复-导入和导出-目录 Oracle安全运行离不开良好的备份和恢复机制,因为我们不是DBA.所以我们也就不过多的讲解DBA的备份和恢复.作为程序员 ...
- Generate Parentheses leetcode java
题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...
- Palindrome Partitioning leetcode java
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- android 框架层 常用类介绍
名称 功能描述 示意图 activitymanager 管理应用程序的周期并提供常用的回退功能 window manager 窗口管理者 content provider 用于访问另一个的数据,或者共 ...
- laravel 5.5 跨域问题解决方案
一.laravel-Cors 安装 在终端执行安装命令如下: composer require barryvdh/laravel-cors 添加服务提供商 在Laravel配置文件app.php的pr ...