Sign in to follow this  
daniel_i_l

stl interator

Recommended Posts

daniel_i_l    295
Lets say I have a stl container (lets say a vector) with an interator pointing to a certain member: inter = container.begin()+5; Now if I erase that member: container.erase(container.begin()+5) then the interator will now be pointing to what before was the 6th member and not NULL. So how do I check if that specific 5th member has been erased or not? For example if I have a different function that needs to know if what had been the 5th member is still there? Please ask if that wasn't clear, it was sort of hard to explain that. Thanks.

Share this post


Link to post
Share on other sites
jyk    2094
Quote:
Original post by daniel_i_l
Now if I erase that member: container.erase(container.begin()+5) then the interator will now be pointing to what before was the 6th member and not NULL.
Actually, after the call to erase() the iterator doesn't necessarily point to anything in particular; it becomes invalid. You can however assign it the return value of erase().

Not sure if I understand the rest of your question.

Share this post


Link to post
Share on other sites
Sharlin    864
You have to keep track of it yourself. The iterator is invalidated after the element it points to is erased, so you cannot do *anything* with it without invoking undefined behaviour. Also remember this:
Quote:
A vector's iterators are invalidated when its memory is reallocated. Additionally, inserting or deleting an element in the middle of a vector invalidates all iterators that point to elements following the insertion or deletion point. It follows that you can prevent a vector's iterators from being invalidated if you use reserve() to preallocate as much memory as the vector will ever use, and if all insertions and deletions are at the vector's end.

Share this post


Link to post
Share on other sites
daniel_i_l    295
I'm not sure exactly how to word this... here goes:
I have a vector (or list if it makes a differance) of structs (vector<SMyStruct>) were each one has a different purpose, lets say according to their position in the vector. Now if I erase one of them (and before that I had an interator pointing to it), it's important for me to be able to check afterwards if that specific one has been erased or not, how do I do that?
Thanks.
EDIT: are the interators to a List invalidated also?

Share this post


Link to post
Share on other sites
jyk    2094
Quote:
Original post by daniel_i_l
I'm not sure exactly how to word this... here goes:
I have a vector (or list if it makes a differance) of structs (vector<SMyStruct>) were each one has a different purpose, lets say according to their position in the vector. Now if I erase one of them (and before that I had an interator pointing to it), it's important for me to be able to check afterwards if that specific one has been erased or not, how do I do that?
The solution is probably context-dependent; it's hard to say without knowing exactly what you're trying to do. In any case, you probably shouldn't be storing iterators (not interator, by the way) to elements in a vector, but should use a list instead, because:
Quote:
EDIT: are the interators to a List invalidated also?
No.

Share this post


Link to post
Share on other sites
Sneftel    1788
They point to the same values (assuming they didn't point to values which were deleted).

BTW, iterator invalidation semantics--among many other things--are covered here. Definitely a useful link to bookmark.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this