keeping an iterator to the last "used" element

Dear all,

I would like to "keep trace" of the "last used" element in a STL container, and I would like to understand what I should expect if the container change (an element is added/removed/changed) and how I can "mark" that the iterator is not "valid".

In particular my plan is, as soon as I create the STL container:
1
2
std::map<A,B> mycont;
std::map<A,B>::const_iterator iter = mycont.end() //to mark an "invalid" state 


1) When elements are added, is iter still pointing to the "real end" of the the container or now is the "end" changed?

2) When I assign the iterator to a specific element, and another element is added, is iter still pointing to the same element?

Thanks a lot,
Matteo
1) I believe it would be invalidated, but I'm not sure. You'd have to go see whether or not insert and those functions invalidate iterators in a map.

2) Same for 1.
IIRC, std::map iterators will still be valid if you insert or erase elements.
Last edited on
Topic archived. No new replies allowed.