Iterators for D

Hasan Aljudy hasan.aljudy at gmail.com
Mon Nov 6 14:00:38 PST 2006



rm wrote:
> Walter Bright wrote:
>> Walter Bright wrote:
>>> .being property returns an iterator that starts at the beginning
>>
>> Make that .begin
>>
>> Also, overloading += will be used to advance the iterator.
> 
> that would give something like this?
> 
> import std.stdio;
> 
> void main()
> {
>     char[] s = "hello";
>     // += for advancing an iterator, why not ++?
>     for (iterator i = s.begin; i != s.end; i += 1)
>         writefln(s[i]);
> 
>     // ??? i is an iterator or a char  ???
>     foreach(i; s)
>         // extend writefln to take iterators ?
>         // but that would mean that it must be possible
>         // to determine a value from an iterator
>         writefln(i);
> }
> 

Why would I use
 >     for (iterator i = s.begin; i != s.end; i += 1)
 >         writefln(s[i]);

instead of:

 >     for (int i = 0; i < s.length; i++)
 >         writefln(s[i]);

?



More information about the Digitalmars-d mailing list