Why is size_t unsigned?
Ali Çehreli
acehreli at yahoo.com
Mon Jul 22 08:30:11 PDT 2013
On 07/22/2013 08:04 AM, monarch_dodra wrote:
> On Monday, 22 July 2013 at 12:51:31 UTC, Andrej Mitrovic wrote:
>> On 7/22/13, JS <js.mdnq at gmail.com> wrote:
>>> foreach doesn't allow you to modify the index to skip over
>>> elements.
>>
>> It does:
>>
>> -----
>> import std.stdio;
>>
>> void main()
>> {
>> int[] x = [1, 2, 3, 4, 5];
>> foreach (ref i; 0 .. 5)
>> {
>> writeln(x[i]);
>> ++i;
>> }
>> }
>> -----
>>
>> Writes:
>> 1
>> 3
>> 5
>
> 99% sure that's unspecified behavior. I wouldn't rely on anything like
> that.
Two more solutions one with foreach one without any explicit looping:
import std.stdio;
import std.range;
void main()
{
int[] x = [1, 2, 3, 4, 5];
foreach (i; iota(0, 5, 2))
{
writeln(x[i]);
++i;
}
writeln(x.indexed(iota(0, x.length, 2)));
}
Ali
More information about the Digitalmars-d-learn
mailing list