The OpenD Programming Language

maxIndex

Computes the index of the first occurrence of range's maximum element.

Complexity: O(range) Exactly range.length - 1 comparisons are needed.

ptrdiff_t
maxIndex
(
alias pred = "a < b"
Range
)
(
Range range
)
if (
isInputRange!Range &&
!isInfinite!Range
&&
is(typeof(binaryFun!pred(range.front, range.front)))
)

Parameters

pred

The ordering predicate to use to determine the maximum element.

range Range

The input range to search.

Return Value

Type: ptrdiff_t

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.

Examples

// 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);

See Also

Meta