转换 C++没有大量新调用的结构数组?

发布于 2024-09-17 17:57:42 字数 1341 浏览 11 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

划一舟意中人 2024-09-24 17:57:42

您可以在 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.

爱的那么颓废 2024-09-24 17:57:42

它很难看,但这会起作用(如果将类型更改为 struct 而不是 class

SomeStruct[] a = new SomeStruct [1000];
a[0].val1 = 0;
a[0].val2 = 1;
a[0].val3 = 2.0;
...
a[999].val1 = 0;
a[999].val2 = 1;
a[999].val3 = 2.0;

编辑:

如果这是一个全局字段,请声明a 作为静态只读

It's ugly, but this will work (if you change the type to struct instead of class)

SomeStruct[] a = new SomeStruct [1000];
a[0].val1 = 0;
a[0].val2 = 1;
a[0].val3 = 2.0;
...
a[999].val1 = 0;
a[999].val2 = 1;
a[999].val3 = 2.0;

Edit:

If this is a global field, declare a as static readonly.

任谁 2024-09-24 17:57:42

您可以通过为 SomeStruct 创建一个新集合(派生自 IEnumerable)来完成此操作。初始化语法中使用的每个项目都会转换为对 .Add(...) 的调用,因此只要您的集合类有一个名为 Add 的方法(不需要为此从任何其他接口继承),并且具有匹配的参数,您就可以使用相同的 C++ 语法。

例如。

public class SomeStructCollection : IEnumerable<SomeStruct> {
    private readonly SomeStruct[] someStructs = new SomeStruct[1000];
    private int currentIndex;

    public void Add(int val1, int val2, double val3) {
        someStructs[currentIndex++] = new SomeStruct(val1, val2, val3);
    }

    public SomeStruct this[int index] {
        get { return someStructs[index];
    }

    //Implement IEnumerable<> interface.
}

然后调用代码可以将值包装在某些块中

SomeStructCollection coll = new SomeStructCollection {
    {0, 0, 0.0}, {1, 1, 1.0}, { ... },
};

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.

public class SomeStructCollection : IEnumerable<SomeStruct> {
    private readonly SomeStruct[] someStructs = new SomeStruct[1000];
    private int currentIndex;

    public void Add(int val1, int val2, double val3) {
        someStructs[currentIndex++] = new SomeStruct(val1, val2, val3);
    }

    public SomeStruct this[int index] {
        get { return someStructs[index];
    }

    //Implement IEnumerable<> interface.
}

Calling code can then wrap values in some blocks

SomeStructCollection coll = new SomeStructCollection {
    {0, 0, 0.0}, {1, 1, 1.0}, { ... },
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文