Group: comp.lang.c++
From: Lasse Reichstein Nielsen
Date: Wednesday, April 09, 2008 4:43 PM
Subject: Re: Iterators in Java and C++

Sam writes:

> For starters, your implementation of next() is wrong. See
> http://java.sun.com/javase/6/docs/api/java/util/Iterator.html. The
> correct, equivalent, implementation would be:
>
> Iter next() const { Iter n; n.ptr=ptr+1; n.end=end; return n }

Actually not. A Java iterator's next() method returns an element,
not an iterator. A closer match would be:

template
struct Iterator {
T::iterator ptr;
T::iterator end;
bool hasNext() const { return ptr != end; }
T next() { return *(ptr++); }
};

so that you can do:

for(Iterator iter = myCollection.iterator(); iter.hasNext();) {
Something element = iter.next();
// ...
}

> Gosh, what the heck are they teaching in college, these days?

Java :)

/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors:
'Faith without judgement merely degrades the spirit divine.'

Safety Articles | Usenet Groups | Usenet News | Bluegrass