How to use sets in D?

Jesse Phillips Jesse.K.Phillips+D at gmail.com
Sun Mar 8 17:58:16 UTC 2020


On Friday, 7 February 2020 at 19:37:08 UTC, mark wrote:
> I am porting code from other languages to D as part of learning 
> D, and I find I've used sets quite a lot. AFAIK D doesn't have 
> a built-in set type or one in the std. lib.
>
> However, I've been perfectly successfully using int[E] where E 
> is my ElementType, and adding with set[element] = 0. I mostly 
> only need add, remove, iteration, and in, with uniqueness what 
> I care most about.
>
> I know I could use bool[E] and set[element] = false, or I 
> suppose container.rbtree. Would either of these--or something 
> else built-in or in the std. lib.--be better?

I think I've usually used the associative arrays, but I also 
think I tend to avoid using this approach but couldn't quite 
remember what I do instead.

I believe I have started just using an array.

arr ~= addMyData;
arr.sort.uniq

Then I make use of the algorithms here.

https://dlang.org/phobos/std_algorithm_setops.html


More information about the Digitalmars-d-learn mailing list