本文介绍了对于哪个标准容器(如果有的话)是由end()persistent返回的迭代器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来快速访问容器中的数据。

I need a way to quickly access data in a container.

所以我记得迭代器的数据位置。容器可以修改(元素添加和删除)之后,但如果我使用容器类型不会使我的迭代器无效(如 std :: map std :: list )我很好。

So I remember iterator of that data position. Container maybe modified (elements added and removed) after that, but if I use container type that does not invalidate my iterator (like std::map or std::list) I am fine.

我的数据也可能不在容器中,所以我设置一个迭代器

Also my data may not be in the container (yet), so I set an iterator to container.end() to reflect that.

哪个标准容器保证 end )在添加和删除元素时不会更改?所以我仍然可以将我的迭代器与 container.end()返回的值进行比较,而不是得到假的否定。

Which standard container guarantees that end() would not change when elements added and removed? So I can still compare my iterator to the value returned by container.end() and not get false negative.

推荐答案

23.2.4 / 9说明关联容器:

23.2.4/9 says of Associative Containers:

现在,有些地方的标准谈到不会使迭代器和对容器元素的引用无效,因此不包括 end()。我不相信这是其中之一 - 我很确定 end()迭代器是一个迭代器到容器。

Now, there are some places where the standard talks about not invalidating "iterators and references to elements of the container", thus excluding end(). I don't believe that this is one of them - I'm pretty sure that an end() iterator is an "iterator to the container".

23.3.5.4/1说 std :: list 表示 insert 不影响迭代器和引用的有效性,23.3.5.4/3说, erase 只使迭代器和对被擦除元素的引用无效。再次, end()迭代器是迭代器,因此它们的有效性不被排除。

23.3.5.4/1 says for std::list that insert "Does not affect the validity of iterators and references", and 23.3.5.4/3 says that erase "invalidates only the iterators and references to the erased elements". Again, end() iterators are iterators and so their validity isn't excluded.

对于任何容器, swap 可以使end()迭代器无效(我假设这是因为有两个自然的行为,要么结束迭代器指向结束的同一个容器,或者是它被交换的一端的结束,但标准不想决定哪个或排除其他可能性)。但你不是交换,只是添加和删除元素。

One thing to watch out for is that for any container, swap can invalidate end() iterators (I assume this is because there are two "natural" behaviors, either that the end iterator points to the end of the same container or else to the end of the one it was swapped with, but the standard doesn't want to dictate which or rule out other possibilities). But you aren't swapping, just adding and removing elements.

这篇关于对于哪个标准容器(如果有的话)是由end()persistent返回的迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 05:05