C ++ 11:在迭代时,是否可以安全地从std :: unordered_map中删除单个元素? [英] C++11: Is it safe to remove individual elements from std::unordered_map while iterating?

查看:486
本文介绍了C ++ 11:在迭代时,是否可以安全地从std :: unordered_map中删除单个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑在迭代时从关联容器中删除元素的规范算法:

  for(auto iter = myMap.begin (); iter!= myMap.end();)
{
if(/ * remove condition * /)
{
iter = myMap.erase
}
else
{
++ iter;
}
}

当使用C ++ 11 std :: unordered_map 容器时。但是,在浏览 std :: unordered_map :: erase /erase\">cppreference.com ,我在阅读以下注释后变得有点担心:


元素的顺序(这可以擦除单个元素,同时迭代容器)(自C ++ 14起)


基于这个语句,我假设有一个语言添加到C ++ 14标准,以确保库实现者保证在 std :: unordered_map ::擦除。例如,也许这样的需求限制了实现从一个元素被移除后不重新散列整个容器,而是只允许它从相应的桶中移除元素?



在C ++ 11中没有这样的保证,如果我希望我的代码是可移植的,我不得不担心一些元素将被访问多次,或者如果我从一个

解决方案

编辑: NoScript的危险性:> std :: unordered_map 我有noscript运行,它显示C11和C14选项卡作为一个框。 Praetorian的答案是正确的,它在实践中得到保证,并在c14中正式化。



** 下面是由于noscript错误。 >

在cplusplus的底部,它表示


只有迭代器和对元素的引用已移除的元素无效。



其余的元素不受影响。



操作将被保留。


http://www.cplusplus.com/reference/unordered_map/unordered_map/erase/



在页面,它声明它是为C ++ 11 ...所以除非他们更新为C ++ 14,我认为它也适用于C ++ 11以及。 Praetorian应该给出一个答案,你应该检查他的答案,因为即使它不是保证在C ++ 11(C ++ 14是这些类的东西的补丁)的标准,这在实践中是保证。 p>

我找不到STL标准,我似乎把它放错了,或者我去看看是否有一个文本保证我可以指向。 : - /


Consider the canonical algorithm for removing an element from an associative container while iterating:

for (auto iter = myMap.begin(); iter != myMap.end(); )
{
    if (/* removal condition */)
    {
        iter = myMap.erase(iter);
    }
    else
    {
        ++iter;
    }
}

I've been applying this algorithm without a second thought when using the C++11 std::unordered_map container. However, after browsing the documentation for std::unordered_map::erase on cppreference.com, I became a little concerned after reading the following note:

The order of the elements that are not erased is preserved (this makes it possible to erase individual elements while iterating through the container) (since C++14)

Based on this statement, I'm assuming there was language added to the C++14 standard to ensure library implementers guarantee ordering after a call to std::unordered_map::erase. For example, maybe such a requirement constrains the implementation from not rehashing the entire container after an element is removed, but rather only allows it to remove the element from its corresponding bucket?

Without such a guarantee in C++11, and if I desire my code to be portable, do I have to worry that some elements will be visited multiple times or not at all if I remove an element from an std::unordered_map during iteration?

解决方案

Edit: The dangers of NoScript. I had noscript running, which displayed the C11 and C14 tabs as one box. Praetorian's answer is correct about it being guaranteed in practice, and formalized in c14.

** Below is wrong due to noscript.

At the bottom of cplusplus it states that

Only the iterators and references to the elements removed are invalidated.

The rest are unaffected.

The relative order of iteration of the elements not removed by the operation is preserved.

http://www.cplusplus.com/reference/unordered_map/unordered_map/erase/

At the top of the page, it states it's for C++11...so unless they updated it for C++14, i think it applies to C++11 as well. Praetorian should put an answer and you should check his for the answer, because even if it's not guaranteed in the standard for C++11 (C++14 being the patch for these sort of things), it's guaranteed in practice.

I couldn't find the STL standard, I seem to have misplaced it, or I'd go see if there was a text guaranteed I could point to. :-/

这篇关于C ++ 11:在迭代时,是否可以安全地从std :: unordered_map中删除单个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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