按照cppreference上的代码,简单测试了下
首先emplace_back和push_back区别在于:
下面是cppreference的例子:
#include
#include
#include
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
#include
#include
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
#include
#include
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.
结论: