从 std::multimap<> 中删除项目后,我可以继续使用迭代器吗?

发布于 2024-07-11 02:44:35 字数 498 浏览 5 评论 0 原文

即使在调用 multimap::erase() 之后,我还可以继续使用 multimap 迭代器吗? 例如:

Blah::iterator iter;
for ( iter = mm.begin();
      iter != mm.end();
      iter ++ )
{
    if ( iter->second == something )
    {
        mm.erase( iter );
    }
}

这是否应该正确运行,或者迭代器在调用擦除后是否失效? 参考站点,例如 http://www.cplusplus.com/reference/stl/multimap /erase.html 对于迭代器的生命周期或迭代器的构造/破坏性方法的影响这个主题出奇地安静。

Can I continue to use an multimap iterator even after a call to multimap::erase()? For example:

Blah::iterator iter;
for ( iter = mm.begin();
      iter != mm.end();
      iter ++ )
{
    if ( iter->second == something )
    {
        mm.erase( iter );
    }
}

Should this be expected to run correctly, or is the iterator invalidated following the call to erase? Reference sites like http://www.cplusplus.com/reference/stl/multimap/erase.html are strangely quiet on this topic of the lifespans of iterators, or the effects of constructive/destructive methods on iterators.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风追烟花雨 2024-07-18 02:44:35

http://www.sgi.com/tech/stl/Multimap.html

Multimap has the important property that inserting a new element
into a multimap does not invalidate iterators that point to existing
elements. Erasing an element from a multimap also does not invalidate
any iterators, except, of course, for iterators that actually point to
the element that is being erased.

所以它应该看起来像这样:

Blah::iterator iter;
for ( iter = mm.begin();iter != mm.end();)
{
    if ( iter->second == something )
    {
        mm.erase( iter++ );
        // Use post increment. This increments the iterator but
        // returns a copy of the original iterator to be used by
        // the erase method
    }
    else
    {
        ++iter;   // Use Pre Increment for efficiency.
    }
}

另请参阅:
如果在从开始到结束迭代时对地图元素调用擦除()会发生

什么

? -the-map-but-the-iterator-must-point-to-the-next-elemen#269717">删除映射中的特定条目,但迭代器必须指向删除后的下一个元素

http://www.sgi.com/tech/stl/Multimap.html

Multimap has the important property that inserting a new element
into a multimap does not invalidate iterators that point to existing
elements. Erasing an element from a multimap also does not invalidate
any iterators, except, of course, for iterators that actually point to
the element that is being erased.

So it should look like this:

Blah::iterator iter;
for ( iter = mm.begin();iter != mm.end();)
{
    if ( iter->second == something )
    {
        mm.erase( iter++ );
        // Use post increment. This increments the iterator but
        // returns a copy of the original iterator to be used by
        // the erase method
    }
    else
    {
        ++iter;   // Use Pre Increment for efficiency.
    }
}

Also see:
What happens if you call erase() on a map element while iterating from begin to end?

and

delete a specific entry in the map,but the iterator must point to the next element after the deletion

梦忆晨望 2024-07-18 02:44:35

C++ 标准 23.1.2.8:

insert 成员不应影响迭代器和对容器的引用的有效性,erase 成员应
仅使迭代器和对已擦除元素的引用无效。

这是所有关联容器的共同要求,std::multimap 就是其中之一。

C++ Standard 23.1.2.8:

The insert members shall not affect the validity of iterators and references to the container, and the erase members shall
invalidate only iterators and references to the erased elements.

This is a common requirement for all associative containers, and std::multimap is one of them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文