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

2024-08-14C/C++开发问题
8

本文介绍了插入 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.

这篇关于插入 STL 映射是否会使其他现有迭代器无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6