我们可以存储unordered_map< T> :: iterator? [英] Can we store unordered_map<T>::iterator?

查看:302
本文介绍了我们可以存储unordered_map< T> :: iterator?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考 http://www.careercup.com/question?id=17188673 by chetan.j9

  void插入(string s){
if(IsElementPresent(s))
return;

myMap [s] = myMapVector.size();
unordered_map< string,int> :: iterator it = myMap.find(s);
myMapVector.push_back(it);问题>我们可以存储unordered_map的迭代器以供以后检索吗?根据我的理解,插入或删除元素后,迭代器将失效。



谢谢

解决方案

@ syam的回答是正确的(+1),但我认为引用唯一的权威来源,C ++ 11标准是有用的:


(§23.2.5/ 13)插入 emplace 成员不应影响对容器元素的引用的有效性,但可能使到容器的所有迭代器无效。擦除成员只能使迭代器和对删除元素的引用无效。



(§23.2.5/ 14) insert emplace 成员不影响迭代器的有效性, z * B,其中N是插入操作之前容器中的元素数量,n是插入的元素数量,B是容器的桶计数,z是容器的最大负载因子。


(在上下文中:§23.2.5是关于无序关联容器的部分,因此它适用于 std :: unordered_set std :: unordered_map std :: unordered_multiset std :: unordered_multimap 。)
这意味着:


  1. n 元素插入 unordered_map ,调用 hash 是否

      hash.size()+ n< hash.max_load_factor()* hash.bucket_count()

    为true。如果它为假,所有迭代器将在插入期间失效。如果为true,迭代器将保持有效。


  2. 即使迭代器在此操作中无效,


  3. 如果你 erase 元素,只有指向这些元素的迭代器才会失效;其他迭代器仍然有效。



Reference http://www.careercup.com/question?id=17188673 by chetan.j9

void Insert( string s ) {
    if( IsElementPresent(s) )
        return;

    myMap[s] = myMapVector.size();
    unordered_map<string,int>::iterator it = myMap.find(s);
    myMapVector.push_back(it);      
}

Question> Can we store the iterator of unordered_map for later retrieval? Based on my understanding, the iterator will be invalidated after the insertion or deletion of an element.

Thank you

解决方案

@syam's answer is correct (+1), but I think it's useful to quote from the only authoritative source, the C++11 Standard:

(§23.2.5/13) The insert and emplace members shall not affect the validity of references to container elements, but may invalidate all iterators to the container. The erase members shall invalidate only iterators and references to the erased elements.

(§23.2.5/14) The insert and emplace members shall not affect the validity of iterators if (N+n) < z * B, where N is the number of elements in the container prior to the insert operation, n is the number of elements inserted, B is the container’s bucket count, and z is the container’s maximum load factor.

(To put this in context: §23.2.5 is the section on unordered associative containers, so it applies to std::unordered_set, std::unordered_map, std::unordered_multiset and std::unordered_multimap.) This means:

  1. If you want to insert n elements into an unordered_map called hash, you can check whether

    hash.size() + n < hash.max_load_factor() * hash.bucket_count()
    

    is true. If it is false, all iterators will be invalidated during the insert. If true, iterators will remain valid.

  2. Even if iterators are invalidated in this operation, references to the elements themselves will remain valid.

  3. If you erase elements, only iterators pointing to those will be invalidated; other iterators will remain valid.

这篇关于我们可以存储unordered_map&lt; T&gt; :: iterator?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆