简体   繁体   English

将unordered_map迭代器更改?

[英]will unordered_map iterator change?

Hi I wonder if the iterator will change if the size of the unordered_map changes and then rehashed? 嗨,我想知道如果unordered_map的大小发生变化然后重新加入,迭代器是否会改变? I'm trying to create a struct of iterator pointers to bring several elements in the unordered_map together. 我正在尝试创建一个迭代器指针结构,以将unordered_map中的多个元素组合在一起。

#include<string>
#include<tr1/unordered_map>

struct linker
{
    unordered_map<Key,T>::iterator it;
    unordered_map<Key,T>::iterator it1;
    unordered_map<Key,T>::iterator it2;

};

unordered_map<string,int> map({{"aaa",1},{"bbb",2},{"ccc",3},{"ddd",4}});

linker node1 = new linker;
node1.it = map.find("aaa");
node1.it1 = &map.find("ccc");
node1.it2 = &map.find("ddd");

map.insert(make_pair({"sss",23}));
.....

after insert too many elements, will the iterator pointer still available and point to the same element/key before the map size changes? 在插入太多元素之后,迭代器指针是否仍然可用并在映射大小更改之前指向相同的元素/键?

C++11 23.2.5/8 "Unordered associative containers": C ++ 11 23.2.5 / 8“无序关联容器”:

Rehashing invalidates iterators, changes ordering between elements, and changes which buckets elements appear in, but does not invalidate pointers or references to elements. 重新散列使迭代器无效,元素之间的顺序更改以及桶元素出现的更改,但不会使指针或对元素的引用无效。

So the iterators would be invalidated on a rehash, but you could take references to the elements instead. 因此迭代器在rehash上会失效,但您可以改为引用元素。

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

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