I'm having trouble with vector iterators. I've read in a few places that checking for null iterators isn't possible, and that the usual way to check iterators is to check it against vector.end() after a search. So for example:
vector< Animal* > animalList;
vector<Animal*>::iterator findInList(const type_info& type)
{
// Loop through list of Animals, if Dog found, return iterator to it
}
auto it = findInList(typeid(Dog));
// With a pointer I can check if it's null, but with an iterator I have to check against animalList.end();
The problem is that the container could be empty. With an iterator I can't return null to indicate the container's empty or the search failed. I can return vector::end(), but cplusplus.com says:
If the container is empty, vector::end() function returns the same as vector::begin()
and then for vector::begin() it says:
If the container is empty, the returned iterator value shall not be dereferenced.
So if I have an empty container, vector::end() and vector::begin() point to the same place, I don't think I can dereference it, and I'm not even sure it's pointing to allocated memory.
Edit: Thanks to everyone. As you have iterated out, vector::end() or vector::begin() do not dereference the iterator, I can safely check against vector::end().