Game Development Community

Writing an iterator - lists of 1 element?

by Daniel Buckmaster · in General Discussion · 08/06/2010 (12:45 pm) · 2 replies

I'm writing an iterator for a list class I've implemented, and I've just run into a great unexpected snag. I'm using it in a for loop, with the condition 'iterator != list->end()'. However, when the list only has 1 element, the 'start' and 'end' elements are the same, so the loop never executes!

How is this generally dealt with?

About the author

Studying mechatronic engineering and computer science at the University of Sydney. Game development is probably my most time-consuming hobby!


#1
08/06/2010 (5:49 pm)
list->end() != list->last()

If list is (for example) at base = new int[1], I think end() should be base + 1, not base.
#2
08/07/2010 (12:11 am)
Ahh, thanks :). I did think of something along those lines in the shower this morning, but then the idea of end() returning random garbage of the end of an array seemed like a bad idea :P. But if it's separated in use and context from last(), that makes sense. Thanks!