- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
抱歉,这变成了关于数组的 3 重问题
我认为(动态)数组在 D 中确实很强大,但以下问题困扰了我一段时间:
在 C++ 中,我可以轻松地分配具有指定值的数组,但在 D 中我还没有找到这样做的方法。肯定没有以下问题:
int[] a = new int[N];
a[] = a0;
0
初始化, 和 2 一样
a0
.可以在 D 中完成类似于以下的操作吗?
int[] a = new int(a0)[N]; // illegal
import std.stdio;
import std.range;
struct S
{
int x;
this(this)
{
writeln("copy ", x);
}
}
void f(S[] s)
{
}
int main()
{
S[] s = new S[10];
foreach (i, ref v; s)
{
v.x = i;
}
f(stride(s, 3)); // error
return 0;
}
f
作为:
f(s, 3);
void f(S[] s, uint stride)
{
ref S get(uint i)
{
assert (i * stride < s.length);
return s[i * stride];
}
for (uint x ... )
{
get(x) = ...;
}
}
get[x]
来代替编写 get(x) ?这样我就可以静态地混入/包含跨步
get
功能并保持其余功能相似。我对所采用的方法很感兴趣,因为不允许本地结构访问函数作用域变量(为什么不呢?)。
最佳答案
But it looks inefficient, since line one will initialize with 0, and like 2 with a0. Could something similar to the following be done in D?
std.array.uninitializedArray
S[] s = uninitializedArray!(S[])(N);
s[] = a0;
Surely I was naive thinking I could simply use stride to create a new array without copying it's elements? There is no way to do so in D, right?
f
有一个
S[]
作为一个论点,这与
stride
不同返回。解决这个问题的 D 方法是让您的
f
函数通过使其成为模板来接受任何范围:
void f(Range)(Range s)
{
foreach (item; s)
// use item
}
S[] s = new S[10];
f(s); // works
f(stride(s, 3)); // works too
f(array(stride(s, 3)));
Would there be a way to instead write get(x) using the index operator get[x]? This way I could statically mixin / include the striding get function and keep the rest of the function similar. I'd be interested in the approach taken, since a local struct is not allowed to access function scope variables (why not?).
struct StrideArray
{
this(S[] s, uint stride) { m_array = s; m_stride = stride; }
S opIndex(size_t i) { return s[i * m_stride]; }
void opIndexAssign(size_t i, S value) { s[i * m_stride] = value; }
private S[] m_array;
private uint m_stride;
}
stride
功能有效。我建议阅读
Ranges .
关于arrays - D 动态数组初始化、stride和索引操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8363728/
我发现可以从 (X, Y) 步幅为 1,图像要求我们将步幅参数指定为 img.strides * 2 或 img.strides + img.strides。 我不知道他们如何在知道编号的情况下快速计
我试图通过参数将一个 Int 常量传递给我的 for 循环的 stride。它采用类型 T.stride。 let notchStep:Int = 5 for degrees:Double i
此代码会引发编译器错误:“参数类型‘StrideTo’应为类或类约束类型的实例” extension Array { func chunks(_ chunkSize: Int) -> [[El
我试图创建一个 Hstack'd 卡片列表,也就是说,我想创建一系列行的 ScrollView 。每行将包含两个并排显示的 View 的 HStack,并由一些列表数据结构初始化。 struct My
我正在尝试将字节数组读入 YuvImage ,但我无法理解 strides 参数的含义。谁能帮忙(最好举个例子?)谢谢。 最佳答案 这里是对 yuv 视频的 stride 的描述: http://ms
我有一个非常大的 numpy 数组(145000 行 * 550 列)。我想在子数组中创建滚动切片。我试图用一个函数来实现它。函数 lagged_vals 的行为符合预期,但 np.lib.strid
该图是未加权且无向的。 给定两个顶点 s 和 t,步幅为 k,我想找到它们之间的最短路径: 长度能被k整除 路径中的每 k 个“步骤”,从第一个开始算起,都是一条简单路径。 意思是,每一步只能“跳转”
我试图理解 OpenMP 破坏循环矢量化的概念原因。此外,解决此问题的任何建议都会有所帮助。我正在考虑手动并行化它来解决这个问题,但这肯定不会很优雅并且会导致大量代码膨胀,因为我的代码包含几个这样的部
我目前正在研究 numpy,在 numpy 中有一个主题叫做“strides”。我明白那是什么。但是它是如何工作的呢?我没有在网上找到任何有用的信息。谁能让我通俗易懂地理解一下? 最佳答案 numpy
我最近了解到 strides在 answer to this post ,并且想知道如何使用它们比我在 this post 中提出的更有效地计算移动平均滤波器(使用卷积过滤器)。 这是我目前所拥有的。
我正在使用 ffmpeg for android 编写简单的视频播放器。以下是我遵循的步骤 从文件 中读取 AVFrame 使用 sws_scale 将 AVFrame 转换为 RGB565 格式 使
我正在尝试从 WPF BitmapSource 获取像素数据目的。据我所知,这可以通过调用它的 CopyPixels 来完成。方法。这个方法需要一个stride参数,不知道怎么获取。据我所知,stri
__global__ void substract(float *A, float *B, float *res, int *n) { int size = *n; int tid =
最近在对经过训练的 pylearn2 模型进行预测时遇到了问题。下面提供了回溯的相关位。我已确保 model.get_input_shape() 与我传递给生成的 theano 预测函数的 numpy
//Defining geometry var vertices = [ +0.0,+0.0,+0.0, +0.0,+0.0,+1.0, -0.5,+0.0,+0.0,
在浏览 pytorch 文档时,我在许多函数中遇到了术语 layout = torch.strided。任何人都可以帮助我了解它在哪里使用以及如何使用。描述说这是返回张量的所需布局。布局是什么意思,有
通常要反转列表应该执行以下操作: >>>>l = [1, 2, 3, 4, 5] >>>>l[::-1] [5, 4, 3, 2, 1] 但不是这样的确切语法: list[::] 如果我不提供可选参
我正在尝试根据教程运行cifar10_train.py,但我得到了 "cifar10_input.py", line 87, in read_cifar10 tf.strided_slice(reco
我理解给定一个可迭代对象,例如 >>> it = [1, 2, 3, 4, 5, 6, 7, 8, 9] 我可以把它变成一个列表,然后在任意点切掉末端,例如 >>> it[1:-2] [2, 3, 4
请有人解释一下(ASCII 真的很受欢迎)stride 参数在 Canvas.drawBitmap() 中代表什么?在 Bitmap.setPixels()/getPixels() ?我知道这是一种跳
我是一名优秀的程序员,十分优秀!