这两个foreach循环将迭代哪些索引:
struct Section{}
Section[] sections;
// assuming we have 10 sections
foreach(ref s; sections[0..$/2+1]) {
// do something
}
foreach_reverse(ref s; sections[$/2..$])
{
// do something
} 发布于 2014-04-05 13:05:32
第一个将在0-sections.length/2+1=6上迭代(独占),第二个将迭代10-5
索引中的$引用数组的长度
array[a..b]表示法扩展为返回数组部分视图的array.opSlice(a,b)。
发布于 2014-04-05 13:16:05
在D中,空结构的大小为1,其基本原理与C++中的相同:有关问题和基本原理,请参见here。因此,您的数组将包含虚拟1字节元素.
让我们检查一下这种行为:
import std.stdio;
struct Section{}
Section[] sections = new Section [10];
void main () {
foreach(ref s; sections[0..$/2+1]) {
writeln (&s);
}
}对我来说,这些指纹:
42208C
42208D
42208E
42208F
422090
422091它们是内存中的连续地址。
https://stackoverflow.com/questions/22881098
复制相似问题