转换 C++没有大量新调用的结构数组?
C++
typedef struct someStruct {
int val1, val2;
double val3;
} someStruct;
someStruct a [1000] = { {0, 0, 0.0}, {1, 1, 1.0}, ... };
据我所知,在 C# 中初始化这样一个表的唯一方法是编写类似的内容
class SomeStruct
{
int val1, val2;
double val3;
public SomeStruct (int val1, int val2, double val3)
{
this.val1 = val1;
this.val2 = val2;
this.val3 = val3;
}
}
SomeStruct[] a = new SomeStruct [1000]
{
new SomeStruct (0, 0, 0.0),
new SomeStruct (1, 1, 1.0),
...
};
Is there a way to have a be a (reference to) an array of types of class SomeClass 而不是指向这些值的指针?
编辑:
重点是我想避免为数组中的每个结构调用 new 。所以我想要的是一个包含 1000 个结构的数组,而不是 1000 个指向 (1000) 结构的指针。我问的原因是,C# 处理这个问题的方式对我来说效率极低,涉及大量内存和内存管理开销(例如 C++)。
我尝试过类似的事情
struct SomeStruct {
int a, b;
double c;
}
SomeStruct[] a = new SomeStruct [1000] { {0,0,0.0}, {1,1,1.0}, ... };
,但那是不可能的。因此,尽管我知道结构体是值类型,但我得出的结论是,只有将它们作为参数传递给函数时才是正确的,而且我必须使用 new,如下所示(此处使用结构体):
struct SomeStruct {
int a, b;
double c;
SomeStruct (int a, int b, double c) {
this.a = a; this.b = b; this.c = c;
}
}
SomeStruct[] a = new SomeStruct [1000] {
new SomeStruct {0,0,0.0},
new SomeStruct {1,1,1.0},
...
};
C++
typedef struct someStruct {
int val1, val2;
double val3;
} someStruct;
someStruct a [1000] = { {0, 0, 0.0}, {1, 1, 1.0}, ... };
The only way to initialize such a table in C# I know of is to write something like
class SomeStruct
{
int val1, val2;
double val3;
public SomeStruct (int val1, int val2, double val3)
{
this.val1 = val1;
this.val2 = val2;
this.val3 = val3;
}
}
SomeStruct[] a = new SomeStruct [1000]
{
new SomeStruct (0, 0, 0.0),
new SomeStruct (1, 1, 1.0),
...
};
Is there a way to have a be a (reference to) an array of values of type class SomeClass instead to pointers to those?
Edit:
The point is that I want to avoid having to call new for each struct in the array. So what I want is an array containg 1000 structs and not 1000 pointers to (1000) structs. The reason I am asking is that the way C# handles this appears insanely inefficent to me, involving a lot of memory and memory management overhead (over e.g. C++).
I had tried something like
struct SomeStruct {
int a, b;
double c;
}
SomeStruct[] a = new SomeStruct [1000] { {0,0,0.0}, {1,1,1.0}, ... };
But that wasn't possible. So though I know that structs are value types, I concluded that this is only true when passing them as parameters to function, and I had to use new, like this (using structs here):
struct SomeStruct {
int a, b;
double c;
SomeStruct (int a, int b, double c) {
this.a = a; this.b = b; this.c = c;
}
}
SomeStruct[] a = new SomeStruct [1000] {
new SomeStruct {0,0,0.0},
new SomeStruct {1,1,1.0},
...
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在 C# 中使用 struct 关键字。 C# 结构体是值类型 - 结构体数组是连续存储的结构体,与 C++ 标准数组相同。
You can use the struct keyword in C#. C# structs are value types- an array of structs is contiguously stored structs, identical to a C++ standard array.
它很难看,但这会起作用(如果将类型更改为
struct
而不是class
)编辑:
如果这是一个全局字段,请声明
a
作为静态只读
。It's ugly, but this will work (if you change the type to
struct
instead ofclass
)Edit:
If this is a global field, declare
a
asstatic readonly
.您可以通过为 SomeStruct 创建一个新集合(派生自
IEnumerable
)来完成此操作。初始化语法中使用的每个项目都会转换为对.Add(...) 的调用
,因此只要您的集合类有一个名为 Add 的方法(不需要为此从任何其他接口继承),并且具有匹配的参数,您就可以使用相同的 C++ 语法。例如。
然后调用代码可以将值包装在某些块中
You can do this by creating a new collection for SomeStruct (derived from
IEnumerable<>
) Each item you use in the initialization syntax gets converted to a call to.Add(...)
, so provided your collection class has a method called Add (doesn't need to inherit from any other interface for this), with matching arguments, you can use the same C++ syntax.eg.
Calling code can then wrap values in some blocks