std.algorithm.comparison.among
- multiple declarations
- Function among
- Template among
Function among
Find
among value
, returning the 1-based index
of the first matching values
value
in
, or values
0
if
is not among value
. The predicate values
pred
is used to
compare values
, and uses equality by default.
Prototype
uint among(alias pred, Value, Values...)( Value value, Values values ) if (Values.length != 0);
Parameters
Name | Description |
---|---|
pred | The predicate used to compare the values . |
value | The value to search for. |
values | The values to compare the value to. |
Returns
0 if value
was not found among
the values
, otherwise the index of the
found value
plus one is returned.
See Also
find and canFind for finding a value
in a
range.
Example
assert(3.among(1, 42, 24, 3, 2)); if (auto pos = "bar".among("foo", "bar", "baz")) assert(pos == 2); else assert(false); // 42 is larger than 24 assert(42.among!((lhs, rhs) => lhs > rhs)(43, 24, 100) == 2);
Example
Alternatively,
can be passed at compile-time, allowing for a more
efficient search, but one that only supports matching on equality:
values
assert(3.among!(2, 3, 4)); assert("bar".among!("foo", "bar", "baz") == 2);
Template among
Find value
among values
, returning the 1-based index
of the first matching value in values
, or 0
if value
is not among values
. The predicate pred
is used to
compare values, and uses equality by default.
Arguments
template among(values...);
Functions
Function name | Description |
---|---|
among |
Parameters
Name | Description |
---|---|
pred | The predicate used to compare the values. |
value | The value to search for. |
values | The values to compare the value to. |
Returns
0 if value was not found among
the values, otherwise the index of the
found value plus one is returned.
See Also
find and canFind for finding a value in a range.
Example
assert(3.among(1, 42, 24, 3, 2)); if (auto pos = "bar".among("foo", "bar", "baz")) assert(pos == 2); else assert(false); // 42 is larger than 24 assert(42.among!((lhs, rhs) => lhs > rhs)(43, 24, 100) == 2);
Example
Alternatively, values
can be passed at compile-time, allowing for a more
efficient search, but one that only supports matching on equality:
assert(3.among!(2, 3, 4)); assert("bar".among!("foo", "bar", "baz") == 2);