假设我有一个列表,其中没有添加或删除新节点。但是,节点可能会被打乱。
保存一个迭代器,指向列表中的一个节点,并在以后任意时间访问它是否安全?
编辑(后续问题): list::splice() 的文档说它从参数列表中删除元素。这是否意味着如果我调用 splice,使用相同的列表作为函数的参数,现有的迭代器将失效?
最佳答案
是的。
迭代器进入列表的标准受赠者不会失效,除非他们指向(比喻地说)的项目从列表中删除。
从这个页面:http://www.sgi.com/tech/stl/List.html
Lists have the important property that insertion and splicing do not
invalidate iterators to list elements, and that even removal invalidates
only the iterators that point to the elements that are removed.
关于c++ - 可以安全地存储 list::iterator 供以后使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/837267/