Which type it better to use for array's indices?
Nicholas Wilson via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Dec 4 05:36:58 PST 2015
On Friday, 4 December 2015 at 13:24:16 UTC, ref2401 wrote:
> Which type it better to use for array's indices?
>
> float[] arr = new float[10];
> int i;
> long j;
> size_t k;
> // which one is better arr[i], a[j]or arr[k] ?
>
> It seem like `size_t` suites well because 'is large enough to
> represent an offset into all addressible memory.'
> (http://dlang.org/spec/type.html#size_t)
> However sometimes I want index to be less the 0 to represent a
> particular case.
> For instance `find(float[] arr, float v)` may return -1 if `v`
> has not been found.
>
> Again which type is better or put it into another words which
> type should I pick as default?
>
> Thank you.
size_t. it is inferred if you use auto. foreach(i,e; arr)
typeof(i)== size_t
Nic
More information about the Digitalmars-d-learn
mailing list