minIndex

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

ptrdiff_t
minIndex
(
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 minimum element.

range Range

The input range to search.

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

Return Value

Type: ptrdiff_t

The index of the first encounter of the minimum element 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

int[] a = [2, 3, 4, 1, 2, 4, 1, 1, 2];

// Minimum is 1 and first occurs in position 3
assert(a.minIndex == 3);
// Get maximum index with minIndex
assert(a.minIndex!"a > b" == 2);

// Range is empty, so return value is -1
int[] b;
assert(b.minIndex == -1);

// Works with more custom types
struct Dog { int age; }
Dog[] dogs = [Dog(10), Dog(5), Dog(15)];
assert(dogs.minIndex!"a.age < b.age" == 1);

See Also

Meta