ljh131
ljh131

Reputation: 113

Is it safe to store pointer of STL list iterator?

I know any insert or remove operations of STL list never invalidate their iterator. However, I'm not sure whether list still preserves iterator itself or not.

So, can I store pointer of list iterator and use it later?

Upvotes: 1

Views: 1548

Answers (1)

Alok Save
Alok Save

Reputation: 206518

For an std::list:

In case of Insertion,
All iterators and references unaffected [23.2.2.3/1]

In case of Erase,
Only the iterators and references to the erased element get invalidated [23.2.2.3/3]

So, Its safe as long as you erase an element and don't use its stored Iterator.

Upvotes: 3

Related Questions