std.algorithm.remove using SwapStrategy.unstable doesn't works

Aleksandar Ružičić ruzicic.aleksandar at gmail.com
Tue Nov 16 04:24:47 PST 2010


I'm trying to use remove() from std.algorithm to remove item from an
array and when I tried to use SwapStrategy.unstable (as I don't need
to maintain order of items in array and I want to optimize where ever
I can) I came to surprising results. In the example below one would
expect that item at index 2 (number 3 in example) will be removed but
instead item at index 0 is removed from array.

Is this a bug or I'm missing a point?

-----------------------------------------------------

import std.stdio, std.algorithm;

void main(string[] args) {

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

	foreach (e; a) writef("%s ", e); writefln("(%s)", a.length); // 1 2 3 4 (4)

	auto i = a.indexOf(3);

	writefln("%s", i);	// 2

	if (i > -1) {
		a = remove!(SwapStrategy.unstable)(a, i);
	}

	foreach (e; a) writef("%s ", e); writefln("(%s)", a.length); // 4 2 3 (3) !?!
}

-----------------------------------------------------

thx in advance,
Aleksandar


More information about the Digitalmars-d mailing list