gpt4 book ai didi

c++ - 如果有插入/删除/重新哈希操作,unordered_map 是否复制/释放包含的对象?

转载 作者:可可西里 更新时间:2023-11-01 18:28:42 28 4
gpt4 key购买 nike

我想在 unordered_map 中存储小对象,只是想知道如果有任何插入/删除/重新哈希操作,它是否可以复制/释放包含的对象?我认为 unordered_map 使用链接列表来存储键/值对,它不应该需要复制/释放像 vector 这样的对象来重新分配内存。

最佳答案

C++11 标准:§ 23.2.5/8

The elements of an unordered associative container are organized into buckets. Keys with the same hash code appear in the same bucket. The number of buckets is automatically increased as elements are added to an unordered associative container, so that the average number of elements per bucket is kept below a bound. Rehashing invalidates iterators, changes ordering between elements, and changes which buckets elements appear in, but does not invalidate pointers or references to elements. For unordered_multiset and unordered_multimap, rehashing preserves the relative ordering of equivalent elements.

unordered_map的简写,
如果是插入/删除操作,

  • 当重新散列发生时,所有迭代器都会失效,但引用不受影响。

关于c++ - 如果有插入/删除/重新哈希操作,unordered_map 是否复制/释放包含的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8664295/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com