Equivilent of STL Set in D ? ...
Lutger
lutger.blijdestijn at gmail.com
Thu Oct 19 16:48:54 PDT 2006
clayasaurus wrote:
> Sean Kelly wrote:
>> clayasaurus wrote:
>>> Hello, I am converting some C++ code which happens to use STL Set to
>>> D. I was thinking of ways to implement set in D, but since I never
>>> got much into STL set in C++ I want to make sure I am thinking right.
>>>
>>> 1) Are D's associative arrays equivalent to STL Set? I've read that
>>> Set is pretty much just a sorted associative array, can this
>>> functionality be achieved in D (using struct as a key type) by
>>> overloading opCmp?
>>
>> No. D's associative array is implemented as a hash table, which is
>> unordered by nature. If you require the elements to be sorted you'll
>> either have to use a different container or copy the values to an
>> array and sort the array before doing whatever you want to do with the
>> sorted values. The word count example does this, for example.
>>
>
> Suppose that I don't need sorting, would an associative array work just
> as well? I'm looking at the code again and it isn't very clear that
> sorting is a requirement, just that I need to have fast insert, lookup
> (and if not in container, add it), and deletion.
>
> Then again, if all that was needed was a hashmap, then I wonder why the
> author didn't use STL map.
iirc stl's map are sorted too, and also use rb-tree usually as
implementation. I think a set in STL is a subset of a map. There is no
hashmap in standard (98) C++.
So for insert, lookup and deletion a set makes sense in STL.
More information about the Digitalmars-d-learn
mailing list