您的位置:首页 > Web前端

Safe to store list::iterator for later use?

2017-10-05 16:54 267 查看
转自: https://stackoverflow.com/questions/837267/safe-to-store-listiterator-for-later-use

===========================Ask======================================

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?

===========================Answer1===================================
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.

===========================Answer2===================================

Yes.

The standard grantees that iterators into a list will not be invalidated unless the item, they point at (metaphorically speaking) is removed from the list.

From this page: 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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: