Eliminates elements at given offsets from range and returns the shortened
range.
For example, here is how to remove a single element from an array:
string[] a = [ "a", "b", "c", "d" ];
a = a.remove(1); // remove element at offset 1assert(a == [ "a", "c", "d"]);
Note that remove does not change the length of the original range directly;
instead, it returns the shortened range. If its return value is not assigned to
the original range, the original range will retain its original length, though
its contents will have changed:
The element at offset 1 has been removed and the rest of the elements have
shifted up to fill its place, however, the original array remains of the same
length. This is because all functions in std.algorithm only change content, not topology. The value 8 is repeated because move was
invoked to rearrange elements, and on integers move simply copies the source
to the destination. To replace a with the effect of the removal, simply
assign the slice returned by remove to it, as shown in the first example.
Multiple indices can be passed into remove. In that case,
elements at the respective indices are all removed. The indices must
be passed in increasing order, otherwise an exception occurs.
The tuple passes in a range closed to the left and open to
the right (consistent with built-in slices), e.g. tuple(1, 3)
means indices 1 and 2 but not 3.
Finally, any combination of integral offsets and tuples composed of two integral
offsets can be passed in:
In this case, the slots at positions 1, 3, 4, and 9 are removed from
the array.
If the need is to remove some elements in the range but the order of
the remaining elements does not have to be preserved, you may want to
pass SwapStrategy.unstable to remove.
In the case above, the element at slot 1 is removed, but replaced
with the last element of the range. Taking advantage of the relaxation
of the stability requirement, remove moved elements from the end
of the array over the slots to be removed. This way there is less data
movement to be done which improves the execution time of the function.
The function remove works on bidirectional ranges that have assignable
lvalue elements. The moving strategy is (listed from fastest to slowest):
Otherwise, elements are moved
incrementally towards the front of range; a given element is never
moved several times, but more elements are moved than in the previous
cases.
Eliminates elements at given offsets from range and returns the shortened range.
For example, here is how to remove a single element from an array:
Note that remove does not change the length of the original range directly; instead, it returns the shortened range. If its return value is not assigned to the original range, the original range will retain its original length, though its contents will have changed:
The element at offset 1 has been removed and the rest of the elements have shifted up to fill its place, however, the original array remains of the same length. This is because all functions in std.algorithm only change content, not topology. The value 8 is repeated because move was invoked to rearrange elements, and on integers move simply copies the source to the destination. To replace a with the effect of the removal, simply assign the slice returned by remove to it, as shown in the first example.
Multiple indices can be passed into remove. In that case, elements at the respective indices are all removed. The indices must be passed in increasing order, otherwise an exception occurs.
(Note that all indices refer to slots in the original array, not in the array as it is being progressively shortened.)
Tuples of two integral offsets can be used to remove an indices range:
The tuple passes in a range closed to the left and open to the right (consistent with built-in slices), e.g. tuple(1, 3) means indices 1 and 2 but not 3.
Finally, any combination of integral offsets and tuples composed of two integral offsets can be passed in:
In this case, the slots at positions 1, 3, 4, and 9 are removed from the array.
If the need is to remove some elements in the range but the order of the remaining elements does not have to be preserved, you may want to pass SwapStrategy.unstable to remove.
In the case above, the element at slot 1 is removed, but replaced with the last element of the range. Taking advantage of the relaxation of the stability requirement, remove moved elements from the end of the array over the slots to be removed. This way there is less data movement to be done which improves the execution time of the function.
The function remove works on bidirectional ranges that have assignable lvalue elements. The moving strategy is (listed from fastest to slowest):