Which type it better to use for array's indices?
Jonathan M Davis via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Dec 4 13:39:34 PST 2015
On Friday, December 04, 2015 13:24:16 ref2401 via Digitalmars-d-learn wrote:
> Which type it better to use for array's indices?
> float arr = new float;
> 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.'
> 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?
In general, you should always use size_t for length unless you have a very
good reason otherwise. Arrays, ranges, and containers all do it. Using
anything else is asking for trouble. So, while you _could_ use something
else for specific use cases, I'd strongly suggest that you not do so.
As for find, std.algorithm already has it. If the range that you give it
does not contain the value you're looking for, then the range it returns is
empty. Otherwise, it returns a range starting at the element you're
searching for. e.g.
auto arr = [1, 7, 42, 99, 2, 5];
auto found1 = arr.find(42);
assert(found1 == [42, 99, 2, 5]);
auto found2 = arr.find(500);
Alternatively, if you want the index, then you can use std.algorithm.count,
in which case, you'd effectively get the 1-based index of what you're
looking for and thus can subtract one from it to get the proper 0-based
If you really do want to return -1, then you can do like std.string.indexOf
does and return ptrdiff_t instead of size_t, but that halves the maximum
array size that you can operate on, which can be a problem in 32-bit
programs which operate on a lot of data but wouldn't be a problem in 64-bit
But in general, size_t is what's used for indices and for length, and using
anything else is potentially asking for trouble - especially if you then mix
that with other code that does use size_t. For instance, some folks have
used int instead of size_t and then gotten bitten when their code was
compiled on 64-bit platforms, because there size_t is ulong instead of uint,
because then all of a sudden code like
int len = arr.length;
won't compile even though it did in 32-bit land. Using size_t as much as
possible for indices and length avoids that sort of problem.
- Jonathan M Davis
More information about the Digitalmars-d-learn