stl interator

Started by
5 comments, last by Sneftel 17 years, 12 months ago
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.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Advertisement
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.
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.
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?
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
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.
So what happens to them?
Thanks
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
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.

This topic is closed to new replies.

Advertisement