创建一个结构体数组的数组?

发布于 2024-12-19 23:59:27 字数 1059 浏览 0 评论 0原文

这个问题是关于结构体数组的语法。

我有一个接受两个整数的结构:

struct point
{
    int x, y;
};

我创建了另一个接受其中 8 个结构的结构:

//Creating an Array of Array of structs
struct Arraypoint
{
    point variable[8];
};
//Not sure if this is the correct way to do it.

现在,在 main 中,我想声明一个 Arraypoint 类型的数组变量code> 具有 8 个索引,因此实际上我将拥有 struct point8 * 8 = 64 元素和 128 整数 (64< /代码> x 和64 y)。

另外,我如何从数组Arraypoint访问单个元素结构点?

好的,在 main 中声明后,假设 Arraypoint 为 2。

Arraypoint arr[2];

如何初始化元素,而无需输入 arr[0].variable[0].x = ... 或不使用 for 循环。 为什么我不能执行以下操作,它似乎不起作用。

Arraypoint arr[2] = {  {(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y)},
                       {(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y)}  }//xy are rand

我在代码中使用了花括号,返回的错误是类型点的初始化器周围缺少大括号类型Arraypoint的初始化器太多

this question is regarding the syntax of an array of array of structs.

I have a struct that takes in two ints:

struct point
{
    int x, y;
};

I have created another struct that takes in 8 of these structs:

//Creating an Array of Array of structs
struct Arraypoint
{
    point variable[8];
};
//Not sure if this is the correct way to do it.

Now, in main, I want to declare an array variable of type Arraypoint with 8 indices, so effectively I will have 8 * 8 = 64 elements of struct point and 128 ints (64 x and 64 y).

Also, how would I access an individual element struct point from the array Arraypoint?

Okay after having declared in main lets say Arraypoint is 2.

Arraypoint arr[2];

How do I initialize the elements without having to type in arr[0].variable[0].x = ... or without using for loops.
Why can't I do the following, it doesn't seem to work.

Arraypoint arr[2] = {  {(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y)},
                       {(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y)}  }//xy are rand

I have used curly braces in my code, the error returned is missing braces around initializer for type point and too many initializers for type Arraypoint.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

倒数 2024-12-26 23:59:27

在 C++ 中,您只需编写:

Arraypoint arr[8];

然后可以通过以下方式访问单个点:

arr[i].variable[j];

不过,更实际的是,您可能最好使用 例如

std::vector<std::vector<point> >

或使用重载的运算符编写自己的类(int i, int j )。例如:

class PointMatrix
{
private:
    std::vector<point> m_points;
public:
    PointMatrix() : m_points(64) {}
    point& operator()(int i, int j) { return m_points[8 * i + j]; }
    const point& operator()(int i, int j) const { return m_points[8 * i + j]; }
};

PointMatrix mat;
m(3, 4).x = 23;

In C++, you'd just write:

Arraypoint arr[8];

An individual point could then be accessed via:

arr[i].variable[j];

More practically, though, you'd probably be better off using e.g.

std::vector<std::vector<point> >

or writing your own class with an overloaded operator(int i, int j). For example:

class PointMatrix
{
private:
    std::vector<point> m_points;
public:
    PointMatrix() : m_points(64) {}
    point& operator()(int i, int j) { return m_points[8 * i + j]; }
    const point& operator()(int i, int j) const { return m_points[8 * i + j]; }
};

PointMatrix mat;
m(3, 4).x = 23;
木格 2024-12-26 23:59:27

明白了:ideone.com/ix3hCArraypoint::variable 必须拥有自己的 { } 对。

struct point
{
    int x, y;
};
#define P {0, 0}

struct Arraypoint
{
    point variable[8];
};
#define V { P, P, P, P, P, P, P, P} 
#define AP { V }  //this is the pair you missed

int main() {
    Arraypoint arr[2] = { AP, AP };
}

got it: ideone.com/ix3hC. Arraypoint::variable has to have it's own { } pair.

struct point
{
    int x, y;
};
#define P {0, 0}

struct Arraypoint
{
    point variable[8];
};
#define V { P, P, P, P, P, P, P, P} 
#define AP { V }  //this is the pair you missed

int main() {
    Arraypoint arr[2] = { AP, AP };
}
烏雲後面有陽光 2024-12-26 23:59:27
struct Arraypoint arraypoints[8];

我想这就是你所追求的。使用它们:

int firstx = arraypoints[0].variable[0].x;

虽然这不是那么漂亮,但

struct point { int x, y; };
struct point[8][8] arraypoints;

可能更好吗?但不知道你到底在追求什么。

struct Arraypoint arraypoints[8];

is what you're after, I think. To use them:

int firstx = arraypoints[0].variable[0].x;

This isn't so pretty though

struct point { int x, y; };
struct point[8][8] arraypoints;

Is probably better? Don't know what exactly you're after though.

原谅过去的我 2024-12-26 23:59:27

要创建 Arraypoints 数组,您可以执行以下操作:

Arraypoint arr[8];

要访问元素:

arr[i]

将返回 i'th Arraypoint 元素

arr[i].variable[j]

将返回 j'元素中的第一个点将

arr[i].variable[j].x

返回该点的 x 坐标。

To create an array of Arraypoints, you can do:

Arraypoint arr[8];

To access an element:

arr[i]

will return the i'th Arraypoint element

arr[i].variable[j]

will return the j'th point in the element

arr[i].variable[j].x

will return the x coordinate of that point.

执妄 2024-12-26 23:59:27

所以我意识到为什么我不能这样声明我的数组,

Arraypoint arr[2] = {  {(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y)},
                   {(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y)}  }
                    //xy are randomn integer values

因为在我的 Arraypoint 结构声明中,它接受了 8 个 point 类型的元素。所以
我必须创建指向存储(x,y)类型的变量,然后我可以将该变量存储在数组点中。

  point point1 = {x,y}, ...;   

  Arraypoint arr[2] = {  {point1,point2,point3,point4,point5,....}  };

仅供将来遇到同样问题的人使用。

So I realized why I couldn't declare my array as such,

Arraypoint arr[2] = {  {(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y)},
                   {(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y)}  }
                    //xy are randomn integer values

its because in my struct declaration of Arraypoint, it takes in 8 elements of type point. So
I have to create variables of type point to store(x,y) and then i could store this variable in Array point.

  point point1 = {x,y}, ...;   

  Arraypoint arr[2] = {  {point1,point2,point3,point4,point5,....}  };

Just for anyone in the future who stumbles across the same problem.

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