Dynamic arrays in D 1.0

Bill Baxter dnewsgroup at billbaxter.com
Sun May 11 12:44:38 PDT 2008


Janice Caron wrote:
> On 11/05/2008, Edward Diener <eddielee_no_spam_here at tropicsoft.com> wrote:
>> In D 1.0 dynamic arrays are the equivalent of C++'s std::vector<T>, with
>> slicing replacing iterators in order to access some subrange of the
>> sequence. In C++ there is functionality in std::vector::erase for erasing
>> elements from the vector and std::vector::insert for inserting new elements
>> into the vector. Where is that functionality in D's dynamic arrays ?
> 
> To erase the elements from index i to index j in place:
> 
>     array[i..$+i-j][] = array[j..$].dup;
>     array = array[0..$+i-j];

It can be done more efficiently using memmove to shift contents down. 
Also if you're dropping the tail end, you can just change .length. 
Cashew's erase routine (called "dropRange") does both.

--bb



More information about the Digitalmars-d mailing list