ma*********@hotmail.com wrote:
Dear Sirs, Dear Newsgroup,
Imagine I have some function that only gives me an iterator to a
vector, but not the vector itself. Unfortunately this vector can be
empty and the iterator can point to 'non valid'.
Yes I know this is rather silly, but I did not write the code...
Normally I know you would check for "iterator != vector.end()", but
since I do not seem to have the vector without changing a lot of old
code, I wonder what to do. Comparing the iterator to 0 does not seem to
be acceptable for the compiler.
The function could not be returning an "iterator" from the client's
point of view, since without knowing how many items are in the
container, the value returned could not be used to iterate the contents
of a container safely.
The function must be returning some kind of reference to an item stored
in a container - the reference could be a pointer to the item itself or
it could even be a pointer to an iterator for the item (which seems to
be the case here).
Since it is possible for the function to return no item, the function
result must be in the form of a pointer (either to the item itself or
to its iterator). The latter approach just adds an extra, unnecessary
layer of indirection. But given the constraints described, the function
has to return a pointer to something (from which the item can be
obtained) in order to allow the function to return NULL (for those
occasions when the function has no item to return).
Greg