emplace_back和push_back的区别
程序员文章站
2022-03-01 23:19:21
...
按照cppreference上的代码,简单测试了下
首先emplace_back和push_back区别在于:
- push_back会先在新的内存中构造临时对象,再调用拷贝构造函数将该对象拷贝到vector的末尾
- emplace_back则直接在vector相应的内存上构造对象,不产生临时对象
下面是cppreference的例子:
#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", 1994);
std::vector<President> reElections;
std::cout << "\npush_back:\n";
reElections.push_back(President("Franklin Delano Roosevelt", "the USA", 1936));
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 1994.
Franklin Delano Roosevelt was re-elected president of the USA in 1936.
可以看到,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", 1994);
std::cout << std::endl;
elections.emplace_back("Nelson Mandela", "South Africa", 1994);
std::cout << std::endl;
elections.emplace_back("Nelson Mandela", "South Africa", 1994);
std::cout << std::endl;
for (President const& president: elections) {
std::cout << president.name << " was re-elected president of "
<< president.country << " in " << president.year << ".\n";
}
}
输出为:
emplace_back:
I am being constructed.
I am being constructed.
I am being moved.
I am being constructed.
I am being moved.
I am being moved.
Nelson Mandela was re-elected president of South Africa in 1994.
Nelson Mandela was re-elected president of South Africa in 1994.
Nelson Mandela was re-elected president of South Africa in 1994.
为什么也调用了多次拷贝构造呢?因为和原本vector中剩下元素的数量对应,猜测因为发生了整体的vector内存搬移
先进行内存reverse,再进行测试看看
#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;
elections.reserve(3);
std::cout << "emplace_back:\n";
elections.emplace_back("Nelson Mandela", "South Africa", 1994);
std::cout << std::endl;
elections.emplace_back("Nelson Mandela", "South Africa", 1994);
std::cout << std::endl;
elections.emplace_back("Nelson Mandela", "South Africa", 1994);
std::cout << std::endl;
for (President const& president: elections) {
std::cout << president.name << " was re-elected president of "
<< president.country << " in " << president.year << ".\n";
}
}
则可看到输出只调用了普通构造函数
emplace_back:
I am being constructed.
I am being constructed.
I am being constructed.
Nelson Mandela was re-elected president of South Africa in 1994.
Nelson Mandela was re-elected president of South Africa in 1994.
Nelson Mandela was re-elected president of South Africa in 1994.
结论:
- emplace_back并不能完全代替push_back,失效场景之后待补
- 尽量使用reserve先请求好够用的空间,对性能的提升很重要
- 要善用cppreference网站,介绍权威,同时示例代码设计很典型,并且可以在线修改编译测试,很有收获
上一篇: c++11 emplace_back和push_back
下一篇: Vector用法
推荐阅读
-
C++ —— 非类中使用const定义常量的初始化,以及#define和typedef的区别
-
微服务和分布式的区别以及微服务框架介绍
-
红米k30t和红米k30i的区别
-
php中用’,'和用’.'的区别
-
[转载] spring aop 环绕通知around和其他通知的区别
-
荣耀magicbook14和magicbook15有什么区别 荣耀magicbook14和15的区别对比
-
块级作用域和var,let,const的区别
-
电子商务系统中 order表中的 order_id 和order_sn的区别
-
Oracle中的Char与Varchar的区别和实例
-
产品运营和产品经理的区别哪个好(产品运营面试问题)