stl interator
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.
Quote:Original post by daniel_i_lActually, 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().
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.
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?
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?
Quote:Original post by daniel_i_lThe 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:
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?
Quote:EDIT: are the interators to a List invalidated also?No.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement