Why Strings as Classes? [C++ iterator]
Fawzi Mohamed
fmohamed at mac.com
Thu Aug 28 06:51:23 PDT 2008
On 2008-08-28 13:06:59 +0200, Michel Fortin <michel.fortin at michelf.com> said:
> On 2008-08-28 05:47:22 -0400, Fawzi Mohamed <fmohamed at mac.com> said:
>
>> An iterator should be like a generator, have a method next, and one
>> at_end or something similar packaged (and maybe prev() and at_start()
>> if it can also go back) in a single struct, furthermore it should work
>> seamlessly with a kind of for_each(x;iterator) construct.
>
> I perfectly agree with this. This is why I prefer much Objective-C's
> NSEnumerator over C++ iterators: they're much simpler to use.
>
> Some time ago, Walter asked if he could change D arrays to have
> internally an end pointer instead of a length value. He said it was to
> allow arrays, or slices, to be used as iterator efficiently. I hope he
> hasn't given up on that idea since slices are much easier to understand
> and use than C++-style iterators.
I agree
>> Instead C++ choose to have begin & end iterators, simply because with
>> that construct it is trivial for the compiler to optimize it for
>> arrays, and you can use pointers as iterators without a
>> cast/constructor.
>
> I would add that if you are using many iterators for the same array
> (vector) in a given algorithm or in function parameters, having only
> one "end" pointer save some space.
ok given, but I would say that one almost never takes advantage of
this, and even then the real advantage is probably small.
More information about the Digitalmars-d
mailing list