ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

关于C中STL容器的问题

2019-07-25 13:06:24  阅读:231  来源: 互联网

标签:multimap unordered-map c c11 stl


> std :: multimap和std :: unordered_multimap是否经常随机播放条目?我问,因为我的代码传递引用以区分具有相同哈希的条目,并且我想知道何时对它们运行引用重定向功能.
>如果我这样做会发生什么:

std::multimap atable; //Type specification stuff left out
//Code that pus in two entries with the same key, call that key foo
int bar = atable[foo];

>如果结果是unordered_multimap,结果会有所不同吗?
>返回传递引用以区分具有相同哈希的条目.有更安全的方法吗?
>如果删除其中一个条目,这些条目是否会移动(这是读取std :: vector文档时的建议)?

解决方法:

不,任何操作过程中都不会损坏任何元素.

正如在this famous Q&A中所解释的,对于关联容器,在插入/擦除时没有迭代器失效(除了当然要擦除的元素).对于无序的关联容器,在重新散列期间存在迭代器失效,标准说明了这一点(强调我的)

23.2.5无序关联容器[unord.req]

9 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< Key,Value>中的Key和Value类型),因为无序映射具有组织为链表的存储桶,以及存储元素的迭代器(键值对) )同时具有元素指针和存储桶指针. reshshing shuffles buckets,它使迭代器无效(因为它们的bucket指针无效),但没有指针或对元素本身的引用.这在another Q&A中有详细说明

标签:multimap,unordered-map,c,c11,stl
来源: https://codeday.me/bug/20190725/1533436.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有