Remove elements without losing capacity
Riccardo M
asd at asd.asd
Tue Oct 4 15:22:18 UTC 2022
Is it possible to remove elements from a range without losing its
capacity?
```
void main()
{
import std.algorithm.mutation : remove, SwapStrategy;
import std.stdio : writeln;
int[] arr = [1, 2, 3, 2, 4, 2, 5, 2];
assert(arr.length == 8);
assert(arr.capacity == 11);
arr = arr.remove!(SwapStrategy.unstable)(0);
assert(arr.length == 7);
assert(arr.capacity == 0);
}
```
Here capacity goes to 0. I wish to prevent future reallocations.
This loses the reserved capacity as well:
```
arr.length--
```
Thanks
More information about the Digitalmars-d-learn
mailing list