In an algorithm I’m currently implementing, I need to manipulate a std::list of struct T.
T holds a reference to another instance of T, but this reference can also be “unassigned”.
At first, I wanted to use a pointer to hold this reference, but using an iterator instead makes it easier to remove from the list.
My question is : how to represent the equivalent to null pointer with my iterator?
I read general solution is to use myList.end(), but in my case, I need to test whether the iterator is “null” or not, and I may add or remove elements to the list between the moment when I store the iterator and the moment I remove it from list… Should I make the iterator point to a known list containing the “null” element? Or is there a more elegant solution?
According to this (emphasis by me):
The same applies to erasure (with the obvious exception of iterators referring to a deleted element becoming invalidated). So yes, obtaining
end()will always point to the same “invalid” element and should be safe to use.