创建一个结构体数组的数组?
这个问题是关于结构体数组的语法。
我有一个接受两个整数的结构:
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 point
的 8 * 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 C++ 中,您只需编写:
然后可以通过以下方式访问单个点:
不过,更实际的是,您可能最好使用 例如
或使用重载的运算符编写自己的类(int i, int j )。例如:
In C++, you'd just write:
An individual point could then be accessed via:
More practically, though, you'd probably be better off using e.g.
or writing your own class with an overloaded
operator(int i, int j)
. For example:明白了:ideone.com/ix3hC。
Arraypoint::variable
必须拥有自己的{ }
对。got it: ideone.com/ix3hC.
Arraypoint::variable
has to have it's own{ }
pair.我想这就是你所追求的。使用它们:
虽然这不是那么漂亮,但
可能更好吗?但不知道你到底在追求什么。
is what you're after, I think. To use them:
This isn't so pretty though
Is probably better? Don't know what exactly you're after though.
要创建
Arraypoints
数组,您可以执行以下操作:要访问元素:
将返回
i
'th Arraypoint 元素将返回
j
'元素中的第一个点将返回该点的 x 坐标。
To create an array of
Arraypoints
, you can do:To access an element:
will return the
i
'th Arraypoint elementwill return the
j
'th point in the elementwill return the
x
coordinate of that point.所以我意识到为什么我不能这样声明我的数组,
因为在我的 Arraypoint 结构声明中,它接受了 8 个 point 类型的元素。所以
我必须创建指向存储(x,y)类型的变量,然后我可以将该变量存储在数组点中。
仅供将来遇到同样问题的人使用。
So I realized why I couldn't declare my array as such,
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.
Just for anyone in the future who stumbles across the same problem.