插入 STL 映射是否会使其他现有迭代器无效?

2022-01-07 00:00:00 c++ stl

我在 STL 中使用了 std::map.我可以在将其他元素插入地图后使用迭代器吗?还有效吗?

I used std::map in STL. Can I use iterator after some other element inserted to the map? Is it still valid?

推荐答案

如果对容器操作的语义有疑问,请咨询 文档:

When in doubt as to the semantics of an operation on a container, consult the documentation:

Map 有一个重要的特性,即在 map 中插入新元素不会使指向现有元素的迭代器失效.

Map has the important property that inserting a new element into a map does not invalidate iterators that point to existing elements.

map 中擦除元素也不会使任何迭代器失效,当然,实际上指向被擦除元素的迭代器除外.

Erasing an element from a map also does not invalidate any iterators, except, of course, for iterators that actually point to the element that is being erased.

这取自 SGI STL 文档.虽然本文档在技术上没有指定 C++ 标准库容器的行为,但除了 STL 中不属于 C++ 标准库的部分之外,这些差异通常是微不足道的.

This is taken from the SGI STL documentation. While this documentation technically does not specify the behavior of the C++ Standard Library containers, the differences are generally insignificant, aside from the parts of the STL that are not part of the C++ Standard Library, of course.

SGI STL 文档是必不可少的参考,尤其是在您没有 C++ 标准副本的情况下.

The SGI STL documentation is an indispensable reference, especially if you don't have a copy of the C++ Standard.

相关文章