On Friday, 21 October 2022 at 22:03:53 UTC, 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) ?
Are you looking for something like this?
```D
import std;
void main() {
int[string] map;
foreach (v ; 1 .. 9)
map[v.to!string] = v;
writeln(map); // prints ["8":8, "4":4, "7":7, "6":6, "1":1,
"5":5, "2":2, "3":3]
auto something = "6";
auto pointer_to_something = (something in map);
if (pointer_to_something) {
writeln(*pointer_to_something); // prints 6
foreach (k, ref v ; map) {
if (&v == pointer_to_something)
break;
writeln(v); // prints 8 4 7
}
}
}
```
This code finds something and then iterates elements between the
"middle" and "begin". You asked for iterating between the
"middle" and "end", but I don't see a big difference because the
elements order is not guaranteed anyway. And just like Steven,
I'm curious about what kind of practical utility is this supposed
to have?