【发布时间】:2019-10-29 07:21:55
【问题描述】:
所以我试图将数组存储在另一个数组中,但我很难将所有值打印出来。
不确定这在 c++ 中是否可行,但在 python 中是可行的,并且已经尝试过了。
#define ROW 7
int one[ROW], two[ROW], three[ROW], four[ROW], five[ROW], six[ROW], seven[ROW];
int grid[7];
void initialize() {
for (int i = 0; i < ROW; i++) {
one[i] = 0;
two[i] = 0;
three[i] = 0;
four[i] = 0;
five[i] = 0;
six[i] = 0;
seven[i] = 0;
}
grid[0] = *one;
grid[1] = *two;
grid[2] = *three;
grid[3] = *four;
grid[4] = *five;
grid[5] = *six;
grid[6] = *seven;
}
void print() {
for (int i = 0; i < 7; i++) {
int bro = grid[i];
cout << grid[i] << endl;
for (int elem : grid[i]) {
cout << elem << endl;
}
}
}
我有这个错误:
error: ‘begin’ was not declared in this scope
for (int elem : grid[i]) {
error: ‘end’ was not declared in this scope
for (int elem : grid[i]) {
【问题讨论】:
-
与其拥有 7 个并行数组,不如拥有一个包含 7 个整数的结构的数组。
-
另一个想法是有一个二维矩阵。使用 2D 矩阵,您无需指定每个变量的名称。
-
我支持@molbdnilo 的评论。如果您正在使用它,请提供一个minimal reproducible example 来重现您的问题。
-
您发布的代码中既没有“开始”也没有“结束”。请提供更多背景信息。
-
grid[i]是 oneint,而不是数组。*one、*two等等也是如此。考虑投资一本好书;通过反复试验学习既费时又会滋生迷信。