Use .get() in MultiD Assoc Array?

Jonathan M Davis jmdavisProg at gmx.com
Thu Aug 30 12:30:31 PDT 2012


On Thursday, August 30, 2012 20:57:44 Paul wrote:
> Maybe I'm not going about my project from the best angle?
> Another problem I have is when I go to printout my array, being
> associative, it is not in the order I built it. It would help
> greatly if I could print it in order.

Hash tables (and an associative array is a hash table) are unordered, so 
you're not going to get them out in the order that you put them in or anything 
like that.

If you use std.container.RedBlackTree (which defaults to being a set but can 
be used as a map if you adjust its comparison predicate appropriately), then 
you have ordering, but it's ordered according to its predicate, not the 
insertion order.

I believe that if you want a map (be it ordered or unordered) to give you 
items back in the order that you inserted them, then a separate list is 
required (be it integrated into the container or something you do alongside 
it) where that list has the keys in the order that they were inserted into the 
container.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list