简体   繁体   English

std :: set和std :: unordered_set如何使用emplace()构造元素?

[英]How does std::set and std::unordered_set construct elements in place with emplace()?

The documentation for both containers say that emplace() function constructs elements in place, but how do they know the location of the new element before the element is constructed? 两个容器的文档都说emplace()函数构造了元素,但是在元素构造之前它们如何知道新元素的位置?

For example, unordered_set places elements according to their hash value. 例如, unordered_set根据其哈希值放置元素。 How does the unordered_set know the hash value of the element before it is constructed? unordered_set如何在构造之前知道元素的哈希值?

I thought maybe the emplace function is meant to take rvalues, calculate the position of the new element and just move the object, but then insert() can do the same thing. 我想,也许在布设功能是为了取右值,计算新元素的位置,只是移动对象,但然后插入()可以做同样的事情。

它没有明确说明它在规范中是如何工作的,但一般情况下会发生的是数据结构 - 内部节点对象(rb-tree节点或包含该值的散列桶节点)将从参数构造,然后该节点将是链接到数据结构(进入集合的rb-tree,进入unordered_set的散列桶),并且如果该值已经存在(因此未添加),节点对象将被销毁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM