AA.remove in foreach && AA = new vs cleaning

Steven Schveighoffer schveiguy at yahoo.com
Thu Oct 22 04:46:13 PDT 2009


On Thu, 22 Oct 2009 02:59:13 -0400, Saaa <empty at needmail.com> wrote:

> Saaa wrote:
>> Steven Schveighoffer wrote
>>
>>>> But I've changed everything to `new` the array afterwards as I deleted
>>>> all  the
>>>> keys anyways.
>>>
>>> Not sure what you mean here.
>>
>> foreach (K k, ; aa)
>> {
>>  ..
>>  //aa.remove(k);
>> }
>> aa = new int[char];
>
> Error: new can only create structs, dynamic arrays or class objects
>
> So, what is the fastest way to clean an AA?

aa = null;

However, if you have multiple copies of the AA, this does not clear out  
the data.  It merely resets the AA reference.

I'd not trust your code because it's undefined behavior.  If you want to  
remove all the elements individually, copy the keys to an array, then  
iterate over the array removing each element.

Or use a real collection class, such as Tango's or dcollections' and use  
the clear() method :)

-Steve


More information about the Digitalmars-d-learn mailing list