简体   繁体   中英

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?

For example, unordered_set places elements according to their hash value. How does the unordered_set know the hash value of the element before it is constructed?

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的散列桶),并且如果该值已经存在(因此未添加),节点对象将被销毁。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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