c# - 将 C++ 数组移植到 C# 数组

标签 c# c++ arrays multidimensional-array porting

我正在移植这个 SimplexNoise从 C++ 到 C# 的模块化,并且遇到了程序员从数组中检索值的方式的问题。他有如下内容:

单工噪声.h

static const int grad3[12][3] = {
    {1,1,0}, {-1,1,0}, {1,-1,0}, {-1,-1,0},
    {1,0,1}, {-1,0,1}, {1,0,-1}, {-1,0,-1},
    {0,1,1}, {0,-1,1}, {0,1,-1}, {0,-1,-1}
};

simplesxnoise.cpp

n1 = t1 * t1 * dot(grad3[gi1], x1, y1);

在我的 C# 端口中:

SimplexNoise.cs

    private static int[][] grad3 = new int[][] { new int[] {1,1,0}, new int[] {-1,1,0}, new int[] {1,-1,0}, new int[] {-1,-1,0},
                                                 new int[] {1,0,1}, new int[] {-1,0,1}, new int[] {1,0,-1}, new int[] {-1,0,-1},
                                                 new int[] {0,1,1}, new int[] {0,-1,1}, new int[] {0,1,-1}, new int[] {0,-1,-1}};

...

    n1 = t1 * t1 * dot(grad3[gi1], x1, y1);

对于我得到的那一行,无法从 int[] 转换为 int。这是合乎逻辑的,但这在 C++ 版本中怎么没有任何错误?我只知道 C++ 的基础知识,但据我所知,这是尝试用一维 int 数组分配整数变量,这没有任何意义。

有什么想法吗?

最佳答案

这是因为根据您链接的来源,dot() 需要一个数组作为它的第一个参数:

float dot(const int* g, const float x, const float y);

const int* g 表示“指向整数的指针”或“整数数组”。考虑到用法,它是签名暗示的“整数数组”。因此,您需要更改 C# dot() 的签名:

float dot(int g[], float x, float y);

关于c# - 将 C++ 数组移植到 C# 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9552561/

相关文章:

c++ - 从动态创建的数组构造 POD 对象时,是否建议使用 placement new?

c++ - 无法使用 Atmel 寄存器编译 Arduino 代码

c# - 如何比较两个字符串数组的序列

c# - 将自定义对象添加到 JObject 时出错

c# - CompositeCommand 等待所有子命令完成

c++ - 通过安装 intel parallel X studio 使用 Cilk 库

我可以使用 fgets 来接受字符指针数组中的字符串而不是 scanf

java - 我怎样才能让这个方法返回一个数组?

c# - 使用反射延迟初始化对象

c# - 用 PHP 重写 Rijndael 256 C# 加密代码