View source code
Display the source code in std/algorithm/searching.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function std.algorithm.searching.minIndex

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

ptrdiff_t minIndex(alias pred, Range) (
  Range range
)
if (isInputRange!Range && !isInfinite!Range && is(typeof(binaryFun!pred(range.front, range.front))));

Parameters

NameDescription
pred The ordering predicate to use to determine the minimum element.
range The input range to search.

Complexity

Ο(range.length) Exactly range.length - 1 comparisons are needed.

Returns

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 maxElement for examples on how to cope with NaNs.

See Also

maxIndex, min, minCount, minElement, minPos

Example

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

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

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

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

Authors

Andrei Alexandrescu

License

Boost License 1.0.