本文介绍了安全存储list :: iterator以供以后使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个列表,其中没有添加或删除新的节点。



是否可以安全地保存迭代器,指向列表中的节点,并在以后任意时间访问它? / p>

编辑(后续问题):
list :: splice()的文档说它从参数列表中删除元素。这是否意味着如果我调用splice,使用相同的列表作为函数的参数,现有的迭代器将无效?

解决方案

是的, std :: list 迭代器只是指向节点的指针。您可以插入,删除(其他节点),并重新排列列表中的节点,并且迭代器不会失效。


Suppose I have a list, in which no new nodes are added or deleted. However, the nodes may be shuffled around.

Is it safe to save an iterator, pointing to a node in the list, and access it at some arbitrarily later time?

Edit (followup question):The documentation for list::splice() says that it removes elements from the argument list. Does this mean if I call splice, using the same list as arguments to the function, that existing iterators will be invalidated?

解决方案

Yes, std::list iterators are just pointers to a node. You can insert, delete (other nodes), and rearrange nodes in the list and the iterator is not invalidated.

这篇关于安全存储list :: iterator以供以后使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 11:50