For any STL container that I'm using, if I declare an iterator (of this particular container type) using the iterator's default constructor, what will the iterator be initialised to?
For example, I have:
std::list<void*> address_list;
std::list<void*>::iterator iter;
What will iter be initialised to?
The default constructor initializes an iterator to a singular value:
By convention a "NULL iterator" for containers, which is used to indicate no result, compares equal to the result of
container.end()
.However, since a default-constructed container iterator is not associated with any particular container, there is no good value it could take. Therefore it is just an uninitialized variable and the only legal operation to do with it is to assign a valid iterator to it.
For other kinds of iterators this might not be true. E.g in case of
istream_iterator
, a default-constructed iterator represents (compares equal to) anistream_iterator
which has reached the EOF of an input stream.The iterator is not initialized, just as
int x;
declares an integer which isn't initialized. It does not have a properly defined value.