Thread overview | |||||
---|---|---|---|---|---|
|
March 30, 2016 key in aa.keys, but aa[key] give range violation? | ||||
---|---|---|---|---|
| ||||
My code looks something like this: bool[ulong][ulong] edge; foreach(u; from) foreach(v; to_) edge[u][v] = true; foreach(u; edge.keys) { auto adj = edge[u]; //.... } And sometimes edge[u] would give Range violation error. |
March 30, 2016 Re: key in aa.keys, but aa[key] give range violation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yuxuan Shui | On Wednesday, 30 March 2016 at 00:26:49 UTC, Yuxuan Shui wrote:
> My code looks something like this:
>
> bool[ulong][ulong] edge;
> foreach(u; from)
> foreach(v; to_)
> edge[u][v] = true;
> foreach(u; edge.keys) {
> auto adj = edge[u];
> //....
> }
>
> And sometimes edge[u] would give Range violation error.
I guess I'm not supposed to do aa.remove() while iterate through it?
|
March 29, 2016 Re: key in aa.keys, but aa[key] give range violation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yuxuan Shui | On 03/29/2016 05:29 PM, Yuxuan Shui wrote:
> On Wednesday, 30 March 2016 at 00:26:49 UTC, Yuxuan Shui wrote:
>> My code looks something like this:
>>
>> bool[ulong][ulong] edge;
>> foreach(u; from)
>> foreach(v; to_)
>> edge[u][v] = true;
>> foreach(u; edge.keys) {
>> auto adj = edge[u];
>> //....
>> }
>>
>> And sometimes edge[u] would give Range violation error.
>
> I guess I'm not supposed to do aa.remove() while iterate through it?
That was my first guess. No, you cannot mutate the container itself while iterating over it.
However, the sample that you've shown should be fine as edge.keys is an eagerly generated array of keys. In that case, you can remove from the aa. Perhaps your actual code is different?
Ali
|
Copyright © 1999-2021 by the D Language Foundation