std.container & ranges

Tobias Pankrath tobias at pankrath.net
Mon Oct 31 00:16:50 PDT 2011


Jonathan M Davis wrote:

>  find allows
> you to do that just fine, and such a remove function would simply be
> duplicating its functionality.

But this thread serves as a proof that it is not obvious and something like 
containers should be obvious to use. Deleting an element with a certain 
value is a common task and should should not take
three function calls, with functions from three different modules.

> You would be doing exactly the same thing in C++ except that it would be
> with an iterator rather than a range. You would use find to find the
> iterator to the element that you wanted and then you'd pass that to the
> list's erase function. 
I don't think we should refer to C++ as an benchmark for ease of use.

How do you delete every occurence of v? Not just the first one? Is this
equally "easy" with find and take? I didn't find an easy way not 
envolving a loop within 10 Minutes by reading the documentation.

What do you think of an construct like this:

foreach(item, itemRange; container)
{
// itemRange is a range only containing item
// itemRange.length == 1 && itemRange.front == item
}

That would be analogous to foreach over array with an index. You might think
that iterating explicity may not be elegant. But it is what people want to
do and it should not be any harder than necessary.

--
Tobias


More information about the Digitalmars-d-learn mailing list