Re: An intelligent iterator for std::list
Thanks exterminator. My code is actually working fine but my original confusion arose because (according to MSDN) std::list::end() returns "a location". I couldn't understand why that location was apparently the same - even after I'd added 3 new elements to the list. I now realise that std::list::end() (at least in VC++) must return something akin to NULL - and that explains why it was the same as the cached iterator, even after adding extra elements.
Re: An intelligent iterator for std::list
Quote:
Originally Posted by John E
I now realise that std::list::end() (at least in VC++) must return something akin to NULL - and that explains why it was the same as the cached iterator, even after adding extra elements.
No, it is not necessarily NULL. Particularly for the STL list with VS2005, end() is actually valid node : _Myhead which is initialized in the function _Buynode(). It is such that the next and prev for it are same as that node itself when the list is empty. Try putting a break point on to that function defined in <list> and see. It could be just about anything by which the library author wants to identify the end().
Re: An intelligent iterator for std::list
Quote:
Originally Posted by exterminator
No! begin() will always return the first element and end() will always return one past the last element...
I think, this describes the original mistake that John E. had in his thinking. begin() will not always return the first element, it will only return the first element for non-empty lists. For empty lists, begin() will return the virtual "one past the last" element, just like end().
A list iterator pointing to the first element will remain pointing to the first element when new elements are added to the list. Likewise a list iterator pointing to the virtual one-past-the-last element, will continue pointing to that element when new elements are added to the list. So, list iterators do remember their position just like they should.
The problem with the original code is that you were expecting the iterator to change from pointing to the one-past-the-last element to pointing to the first element, just because you received the iterator through a call to begin(). Once you understood that begin() does not always return an iterator pointing to the first element, it's easy to realize the code works as expected (and not at all undefined like I stated previously :blush: ).
Re: An intelligent iterator for std::list
Quote:
Originally Posted by treuss
I think, this describes the original mistake that John E. had in his thinking. begin() will not always return the first element, it will only return the first element for non-empty lists. For empty lists, begin() will return the virtual "one past the last" element, just like end().
Yes, I think that's part of the explanation but it's more true to say that I assumed myList.end() would return something different as new elements get added to the list. This seems not to be the case (at least for VC++).