Function std.algorithm.comparison.cmp
Performs three-way lexicographical comparison on two input ranges
according to predicate pred
. Iterating
and r1
in
lockstep, r2
compares each element cmp
e1
of
with the
corresponding element r1
e2
in
. If one of the ranges has been
finished, r2
returns a negative value if cmp
has fewer
elements than r1
, a positive value if r2
has more elements
than r1
, and r2
0
if the ranges have the same number of
elements.
If the ranges are strings,
performs UTF decoding
appropriately and compares the ranges one code point at a time.
cmp
Prototypes
int cmp(alias pred, R1, R2)( R1 r1, R2 r2 ) if (isInputRange!R1 && isInputRange!R2 && !(isSomeString!R1 && isSomeString!R2)); int cmp(alias pred, R1, R2)( R1 r1, R2 r2 ) if (isSomeString!R1 && isSomeString!R2);
Parameters
Name | Description |
---|---|
pred | The predicate used for comparison. |
r1 | The first range. |
r2 | The second range. |
Returns
0 if both ranges compare equal
. -1 if the first differing element of
is less than the corresponding element of r1
according to r2
pred
. 1 if the first differing element of
is less than the
corresponding element of r2
according to r1
pred
.
Example
int result; result = cmp("abc", "abc"); assert(result == 0); result = cmp("", ""); assert(result == 0); result = cmp("abc", "abcd"); assert(result < 0); result = cmp("abcd", "abc"); assert(result > 0); result = cmp("abc"d, "abd"); assert(result < 0); result = cmp("bbc", "abc"w); assert(result > 0); result = cmp("aaa", "aaaa"d); assert(result < 0); result = cmp("aaaa", "aaa"d); assert(result > 0); result = cmp("aaa", "aaa"d); assert(result == 0); result = cmp(cast(int[])[], cast(int[])[]); assert(result == 0); result = cmp([1, 2, 3], [1, 2, 3]); assert(result == 0); result = cmp([1, 3, 2], [1, 2, 3]); assert(result > 0); result = cmp([1, 2, 3], [1L, 2, 3, 4]); assert(result < 0); result = cmp([1L, 2, 3], [1, 2]); assert(result > 0);