(Note: I'm currently learning C++, so if there is a better way to do what I am doing, explanations would be helpful.)
(注意:我目前正在学习C ++,所以如果有更好的方法来做我正在做的事情,解释会有所帮助。)
I am making Conway's Game of Life in C/C++ and have the following code:
我正在使用C / C ++制作康威的生命游戏并拥有以下代码:
bool ** previous;
bool ** current;
void init() {
previous = new bool*[width];
current = new bool*[width];
for (int i =0; i < width; i++) {
previous[i] = new bool[height];
current[i] = new bool[height];
}
}
The reason for the dynamic arrays is that the width and height are given by the user at runtime. If I had one-dimensional arrays, I could do the following:
动态数组的原因是用户在运行时给出了宽度和高度。如果我有一维数组,我可以做以下事情:
bool * previous;
bool * current;
void () {
bool * temp = current;
current = previous;
previous = temp;
}
However, this approach does not work as hoped with the two-dimensional array. As it is technically an array of arrays, do I have to swap each sub-array pointer individually? Is there a better way to go about defining multi-dimensional arrays and swapping them?
然而,这种方法不像二维阵列那样有效。因为它在技术上是一个数组数组,我是否必须单独交换每个子数组指针?有没有更好的方法来定义多维数组并交换它们?
Edit: I have yet to really use any C++ specific features, so if I can do this in pure C, I'd prefer that.
编辑:我还没有真正使用任何C ++特定的功能,所以如果我能用纯C做到这一点,我宁愿这样做。
5 个解决方案
#1
3
There's no real reason here to bother with an array of arrays. It's a ton more work to maintain and destroy properly, especially as you've avoided using the Standard Library containers that would handle a lot of the memory management for you.
这里没有真正的理由来打扰数组。要正确维护和销毁这项工作要多得多,特别是因为您已经避免使用可以为您处理大量内存管理的标准库容器。
It's usually easier to allocate width * height
cells in a single array and reference them like cell[x + width * y]
where x
is your column, y
is your row.
通常更容易在单个数组中分配width * height单元格,并像cell [x + width * y]一样引用它们,其中x是您的列,y是您的行。
Do keep in mind that in the particular case of this problem what you want is an array of bits and not an array of booleans. C++ does have a special case container for a bitset that is worth using: std::vector<bool>
.
请记住,在这个问题的特定情况下,你想要的是一个位数组而不是一个布尔数组。 C ++确实有一个特殊的容器,用于值得使用的bitset:std :: vector
#2
1
This works:
int arr1[10][10],arr2[10][10]
//Swap 2nd row between arrays
std::swap(arr1[2], arr2[2]);
#3
0
If it's C++ use std::vector
's and forget about heap allocations, pure arrays, memcpy for them and copy/assignment operators.
如果它是C ++使用std :: vector并忘记堆分配,纯数组,memcpy和它们以及复制/赋值运算符。
#4
0
Instead of going for your array of arrays, why not use a one-dimensional array of size width*height
?
为什么不使用大小宽度*高度的一维数组,而不是使用数组数组?
Just access your elements by y*width + x
.
只需通过y * width + x访问您的元素。
Allocate the array as usual with malloc(width*height*sizeof(bool))
. This should be sufficient for your problem
像往常一样使用malloc(width * height * sizeof(bool))分配数组。这应该足以解决您的问题
#5
0
While the earlier answers are technically correct, they do not seem to answer your original question. You say "this approach does not work as hoped with the two-dimensional array", but why doesn't the following work?
虽然早期的答案在技术上是正确的,但它们似乎没有回答您原来的问题。你说“这种方法不像二维数组那样有效”,但为什么以下不起作用呢?
void () {
bool ** temp = current;
current = previous;
previous = temp;
}
If you don't mind to use c++ features the std::swap functions comes in handy:
如果您不介意使用c ++函数,那么std :: swap函数会派上用场:
std::swap(current, previous);
#1
3
There's no real reason here to bother with an array of arrays. It's a ton more work to maintain and destroy properly, especially as you've avoided using the Standard Library containers that would handle a lot of the memory management for you.
这里没有真正的理由来打扰数组。要正确维护和销毁这项工作要多得多,特别是因为您已经避免使用可以为您处理大量内存管理的标准库容器。
It's usually easier to allocate width * height
cells in a single array and reference them like cell[x + width * y]
where x
is your column, y
is your row.
通常更容易在单个数组中分配width * height单元格,并像cell [x + width * y]一样引用它们,其中x是您的列,y是您的行。
Do keep in mind that in the particular case of this problem what you want is an array of bits and not an array of booleans. C++ does have a special case container for a bitset that is worth using: std::vector<bool>
.
请记住,在这个问题的特定情况下,你想要的是一个位数组而不是一个布尔数组。 C ++确实有一个特殊的容器,用于值得使用的bitset:std :: vector
#2
1
This works:
int arr1[10][10],arr2[10][10]
//Swap 2nd row between arrays
std::swap(arr1[2], arr2[2]);
#3
0
If it's C++ use std::vector
's and forget about heap allocations, pure arrays, memcpy for them and copy/assignment operators.
如果它是C ++使用std :: vector并忘记堆分配,纯数组,memcpy和它们以及复制/赋值运算符。
#4
0
Instead of going for your array of arrays, why not use a one-dimensional array of size width*height
?
为什么不使用大小宽度*高度的一维数组,而不是使用数组数组?
Just access your elements by y*width + x
.
只需通过y * width + x访问您的元素。
Allocate the array as usual with malloc(width*height*sizeof(bool))
. This should be sufficient for your problem
像往常一样使用malloc(width * height * sizeof(bool))分配数组。这应该足以解决您的问题
#5
0
While the earlier answers are technically correct, they do not seem to answer your original question. You say "this approach does not work as hoped with the two-dimensional array", but why doesn't the following work?
虽然早期的答案在技术上是正确的,但它们似乎没有回答您原来的问题。你说“这种方法不像二维数组那样有效”,但为什么以下不起作用呢?
void () {
bool ** temp = current;
current = previous;
previous = temp;
}
If you don't mind to use c++ features the std::swap functions comes in handy:
如果您不介意使用c ++函数,那么std :: swap函数会派上用场:
std::swap(current, previous);