The ordering predicate to use to determine the maximum element.
The input range to search.
The index of the first encounter of the maximum in range. If the range is empty, -1 is returned.
Limitations: If at least one of the arguments is NaN, the result is an unspecified value. See std.algorithm.searching.maxElement for examples on how to cope with NaNs.
// Maximum is 4 and first occurs in position 2 int[] a = [2, 3, 4, 1, 2, 4, 1, 1, 2]; assert(a.maxIndex == 2); // Empty range int[] b; assert(b.maxIndex == -1); // Works with more custom types struct Dog { int age; } Dog[] dogs = [Dog(10), Dog(15), Dog(5)]; assert(dogs.maxIndex!"a.age < b.age" == 1);
Computes the index of the first occurrence of range's maximum element.
Complexity: O(range) Exactly range.length - 1 comparisons are needed.