Find in assoc array then iterate

Ali Çehreli acehreli at yahoo.com
Fri Oct 21 23:38:21 UTC 2022


On 10/21/22 15:03, Kevin Bailey wrote:
> I'm trying to do this equivalent C++:
> 
>      unordered_map<string, int> map;
> 
>      for (auto i = map.find(something); i != map.end(); ++i)
>          ...do something with i...
> 
> in D, but obviously with an associative array. It seems that it's quite
> easy to iterate through the whole "map", but not start from the middle.
> Am I missing something obvious (or not so obvious) ?
> 

Something like the following perhaps, which sorts the keys:

import std; // Sorry :(

void main() {
     auto m = iota(20).map!(i => tuple(i, format!"value_%s"(i))).assocArray;

     foreach (key; m.keys.sort.find(13)) {
         writeln(m[key]);
     }
}

Ali



More information about the Digitalmars-d-learn mailing list