How to detect the last element in a for loop
Bryan Olson
fakeaddress at nowhere.org
Mon Jul 29 10:29:43 EDT 2002
Tom Verbeure wrote:
[...]
>> > Given that you have an explicit iterator, wouldn't it be trivial to
>>add an
>> > 'end()' method to this iterator to indicate the end of the sequence
>>(just
>> > like C++ iterators) ?
>> >
>> > This would result in:
>> >
>> > for item in iterator:
>> > if iterator.next().end():
>> > do_something_special
>> >
>> > do_something_for_all
>>
>>I think you've misunderstood the end() value in C++ STL iterators. It
>>is not the last item, but one past the last item. The iterator can take
>>the end value, but dereferencing the end value is illegal.
>
>
> That would be the case if I would check for iterator.end(), but I
check for
> iterator.next().end() !
For a given C++ STL iterator, end() will return the *same* value no
matter how many times you advance the iterator. It looks like you
wanted:
...
if iterator.next() == iterator.end():
do_something_special
--Bryan
More information about the Python-list
mailing list