Har*_*hah 26 c++ unordered-map c++11
std::unordered_map::emplace和std::unordered_map::insertC++有什么区别?
Chr*_*rew 43
unordered_map::insert将键值对复制或移动到容器中.它被重载以接受引用到const或rvalue引用:
std::pair<iterator,bool> insert(const std::pair<const Key, T>& value);
template<class P>
std::pair<iterator,bool> insert(P&& value);
Run Code Online (Sandbox Code Playgroud)
unordered_map::emplace允许您通过构建元素来避免不必要的副本或移动.它使用完美转发和可变参数模板将参数转发给键值对的构造函数:
template<class... Args>
std::pair<iterator,bool> emplace(Args&&... args);
Run Code Online (Sandbox Code Playgroud)
但是这两个功能之间存在很多重叠.emplace可以用来转发键值对的复制/移动构造函数,这样就可以像使用它一样使用它insert.这意味着使用emplace并不能保证您将避免复制或移动.另外insert,采用rvalue-reference 的版本实际上是模板化的,并且接受任何类型P,使得键值对可以构造P.
原则上,安置功能有时应该比插入功能更有效,并且它们的效率永远不会低.
(编辑: Howard Hinnant进行了一些实验,有时显示insert速度比emplace)
如果您确实想要复制/移入容器,那么使用它可能是明智的,insert因为如果传递不正确的参数,则更有可能出现编译错误.您需要更加小心,将正确的参数传递给安置函数.
大多数实现unordered_map::emplace将导致为新对动态分配内存,即使映射包含已具有该键的项目并且emplace将失败.这意味着如果很可能emplace失败,您可以使用insert获得更好的性能,以避免不必要的动态内存分配.
小例子:
#include <unordered_map>
#include <iostream>
int main() {
auto employee1 = std::pair<int, std::string>{1, "John Smith"};
auto employees = std::unordered_map<int, std::string>{};
employees.insert(employee1); // copy insertion
employees.insert(std::make_pair(2, "Mary Jones")); // move insertion
employees.emplace(3, "James Brown"); // construct in-place
for (const auto& employee : employees)
std::cout << employee.first << ": " << employee.second << "\n";
}
Run Code Online (Sandbox Code Playgroud)
Edit2:根据要求.也可以使用unordered_map::emplace带有多个构造函数参数的键或值.使用std::pair 分段构造函数,您仍然可以避免不必要的副本或移动.
#include <unordered_map>
#include <iostream>
struct Employee {
std::string firstname;
std::string lastname;
Employee(const std::string& firstname, const std::string& lastname)
: firstname(firstname), lastname(lastname){}
};
int main() {
auto employees = std::unordered_map<int, Employee>{};
auto employee1 = std::pair<int, Employee>{1, Employee{"John", "Smith"}};
employees.insert(employee1); // copy insertion
employees.insert(std::make_pair(2, Employee{"Mary", "Jones"})); // move insertion
employees.emplace(3, Employee("Sam", "Thomas")); // emplace with pre-constructed Employee
employees.emplace(std::piecewise_construct,
std::forward_as_tuple(4),
std::forward_as_tuple("James", "Brown")); // construct in-place
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18850 次 |
| 最近记录: |