Re: Iterators in Java and C++
On 5 avr, 15:59, Erik Wikstr=F6m <Erik-wikst...@telia.com> wrote:
On 2008-04-05 14:45, Sam wrote:
[...]
Don't know if you posted the correct link or if they just
teaches us to read better in college these days. :-) I think
the correct implementation, given the link you posted, should
be
T& next() { ptr++; return *ptr; }
Which also shows the problem that James Kanze pointed out,
namely that you can not dereference the iterator more than
once without changing what it refers to.
T& next() { return *ptr ++ ; }
would be the correct implementation in C++. Except, of course,
that incrementing an iterator generally involves a lot more than
just incrementing a pointer. The whole performance discussion
is just so much hand waving to avoid discussing the real issues;
in practice, the only thing that could possibly cause a
measurable difference is the fact that Java uses virtual
functions, and even that can often be optimized away with a JIT
compiler. More to the point, of course, is that the work
arounds you need to make the STL iterators work (extra
variables, etc.) will often outweigh the cost of the virtual
function calls.
But of course, both patterns will be sufficiently fast for 99.9%
of any use. What really counts is the amount of extra
programmer work you have to do, and the loss of design
cleanliness and understanding that is introduced. And when
performance is an issue, of course, the time you save using a
well designed iterator will give you more time to address the
issue, and solve it correctly. If speed is important, you need
to encapsulate, profile and then attack the critical points.
And you need the time to do it, time which shouldn't be wasted
on premature optimization (which generally has a negative effect
on performance in the long run, because it so often breaks
encapsulation).
Ideally you would have two methods, one to increment and one to
dereference, like so:
typename T::value_type& getValue() const { return *ptr; }
void moveNext{ ptr++; }
Rewriting it all in a bit my C++ stylish way we get:
#include <vector>
#include <iostream>
template <class T>
struct Iter {
typedef typename T::iterator STLIter;
typedef typename T::value_type Val;
STLIter ptr, end;
Iter(STLIter begin, STLIter end) : ptr(begin), end(end) {}
operator bool() const { return ptr != end; }
Val& operator*() const { return *ptr; }
void operator++() { ptr++; }
};
Except that I don't really like the abuse of operator
overloading. What's wrong with:
template< typename T >
class Iter
{
public:
Iter( Container& c ) ;
bool isDone() const ;
T& element() const ;
void next() ;
}
int main()
{
std::vector<int> v;
for (int i = 0; i < 10; ++i)
v.push_back(i);
Iter<std::vector<int> > it(v.begin(), v.end());
while (it)
{
std::cout << *it;
++it;
}
}
Not too sure about the operator bool() thing though...
I am:-). It's definitly an abuse. (But I've seen worse; I once
saw someone recommend overloading unary plus. So that the loop
would be:
for ( Iter i( container ) ; +i ; ++ i ) {
// ...
}
I far prefer nice clear names that say exactly what the function
is doing. Overloading ++ is arguable both ways, but the others
are blatant abuse.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34