安全存储list :: iterator供以后使用?

8 c++ iterator stl list

假设我有一个列表,其中没有添加或删除新节点.但是,节点可能会被混乱.

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

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

Mar*_*ork 25

是.
除非他们指向(隐喻地说)的项目从列表中删除,否则将迭代到列表中的标准被授权者将不会失效.

从这个页面: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.
Run Code Online (Sandbox Code Playgroud)


Jam*_*ton 3

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

  • 不能保证迭代器是指针。事实上,您可能会认为 std::list<>::iterator 不是指针,因为它需要对所有迭代器操作(例如 ++)进行智能处理 (10认同)