emplace_back与push_back的区别
std::vector::emplace_back
template< class... Args > void emplace_back( Args&&... args ); |
(since C++11) | |
Appends a new element to the end of the container. The element is constructed in-place, i.e. no copy or move operations are performed. The constructor of the element is called with exactly the same arguments that are supplied to the function.
If the new size() is greater than capacity() then all iterators and references (including the past-the-end iterator) are invalidated. Otherwise only the past-the-end iterator is invalidated.
Parameters
args | - | arguments to forward to the constructor of the element |
Return value
(none)
Complexity
Constant.
Exceptions
If an exception is thrown, this function has no effect (strong exception guarantee). If T
's move constructor is not noexcept and is not CopyInsertable
into *this
, vector will use the throwing move constructor. If it throws, the guarantee is waived and the effects are unspecified.
Notes
The specialization std::vector<bool> did not have emplace_back()
member until C++14.
Example
The following code uses emplace_back
to append an object of type President
to a std::vector. It demonstrates how emplace_back
forwards parameters to the President
constructor and shows how using emplace_back
avoids the extra copy or move operation required when using push_back
.
#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(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) = default;
}; 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";
}
}
Output:
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 .
Franklin Delano Roosevelt was re-elected president of the USA in .
emplace_back与push_back的区别的更多相关文章
- 学习 emplace_back() 和 push_back 的区别 emplace_back效率高
在引入右值引用,转移构造函数,转移复制运算符之前,通常使用push_back()向容器中加入一个右值元素(临时对象)的时候,首先会调用构造函数构造这个临时对象,然后需要调用拷贝构造函数将这个临时对象放 ...
- emplace_back() 和 push_back 的区别(转)
在引入右值引用,转移构造函数,转移复制运算符之前,通常使用push_back()向容器中加入一个右值元素(临时对象)的时候,首先会调用构造函数构造这个临时对象,然后需要调用拷贝构造函数将这个临时对象放 ...
- 【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的效率要高很多. 首先,写 ...
随机推荐
- c#字符串及数组操作
字符串操作(取当前时间)string time=convert.tostring(DateTime.Today).split( new char []{' '}); textbox1.text= ...
- 刑事案件的构成要素 zt
论刑事案件的构成要素 马忠红 2013-03-22 14:05:33 来源:<中国人民公安大学学报:社会科学版>(京)2012年5期 [内容提要]刑事案件是由诸多要素构成的一个系 统. ...
- HDU 5606 tree 并查集
tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ansi=size[findset(i)],size表示每个并 ...
- 017QTP 描述性编程的使用方法
一.什么时候使用描述性编程 在测试过程中,有些界面元素是动态出现或动态变化的,在录制的时候并没有添加到对象库中 二.描述性编程的运行原理 用描述性编程编写的测试脚本在运行时,QTP会使用测试脚本中给出 ...
- qtp不识别树结构中的点击事件
qtp不识别树结构中的点击事件,未生成该点击事件的脚本,解决办法: 1.未生成点击"auto分类c1"的脚本 2.点击1.对象库-2.添加对象库-3.选中对象-点击OK,即将该对象 ...
- tomcat 容器中的UML架构图
- 【UR #12】实验室外的攻防战(BIT)
[题目链接] http://uoj.ac/problem/180 [题意] 给定两个1..n的排列AB,只有当ai<ai+1才能交换ai和ai+1,问是否能够将A转换为B. [思路] 令a[i] ...
- n & (n-1)
n&(n-1)作用:将n的二进制表示中的最低位为1的改为0,先看一个简单的例子: n = 10100(二进制),则(n-1) = 10011 ==>n&(n-1) = 10000 ...
- English Morphology
最近参与一个小project,需要编写一个针对英文单词的stem 算法. 1. 最为常见的stem 算法 就是The English (Porter2) stemming algorithm http ...
- HDU-4687 Boke and Tsukkomi 带花树,枚举
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4687 题意:给一个无向图,求所有的最大匹配的情况所不包含的边.. 数据比较小,直接枚举边.先求一次最大 ...