如何动态分配矩阵?
如何在 C++ 中动态分配二维矩阵? 我根据我已经知道的情况进行了尝试:
#include <iostream>
int main(){
int rows;
int cols;
int * arr;
arr = new int[rows][cols];
}
它适用于一个参数,但现在适用于两个参数。我应该怎么办?
How do you dynamically allocate a 2D matrix in C++?
I have tried based on what I already know:
#include <iostream>
int main(){
int rows;
int cols;
int * arr;
arr = new int[rows][cols];
}
It works for one parameter, but now for two. What should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
矩阵
实际上可以表示为数组的数组。当然,要删除矩阵,您应该执行以下操作:
我刚刚想到了另一种可能性:
释放该数组更容易:
该解决方案的优点是为所有元素分配一个大内存块,而不是几个小内存块大块。不过,我发布的第一个解决方案是数组的数组概念的更好示例。
A matrix
is actuallycan be represented as an array of arrays.Of course, to delete the matrix, you should do the following:
I have just figured out another possibility:
Freeing this array is easier:
This solution has the advantage of allocating a single big block of memory for all the elements, instead of several little chunks. The first solution I posted is a better example of the arrays of arrays concept, though.
您还可以使用
std::vectors
来实现此目的:使用: 'std::vector< std::向量>'
例子:
You can also use
std::vectors
for achieving this:using: 'std::vector< std::vector >'
Example:
尝试 boost::multi_array
Try boost::multi_array
如果您不介意语法
或在某处使用运算符[]重载。这可能比数组数组更适合缓存,也可能不是,更可能的是您不应该关心它。我只是想指出a)数组的数组不仅仅是解决方案,b)如果矩阵位于一个内存块中,一些操作更容易实现。例如
短一行
,比向此类矩阵添加行更痛苦的情况
If you either don't mind syntax
or use operator[] overaloading somewhere. This may be more cache-friendly than array of arrays, or may be not, more probably you shouldn't care about it. I just want to point out that a) array of arrays is not only solution, b) some operations are more easier to implement if matrix located in one block of memory. E.g.
one line shorter than
though adding rows to such matrix is more painful
或者您可以只分配一个 1D 数组,但以 2D 方式引用元素:
寻址第 2 行、第 3 列(左上角为第 0 行、第 0 列):
arr[2 * MATRIX_WIDTH + 3]
其中 MATRIX_WIDTH 是元素的数量连续。
or you can just allocate a 1D array but reference elements in a 2D fashion:
to address row 2, column 3 (top left corner is row 0, column 0):
arr[2 * MATRIX_WIDTH + 3]
where MATRIX_WIDTH is the number of elements in a row.
这是最清楚的&我知道在 C++ 中分配动态二维数组的直观方法。本示例中的模板涵盖了所有情况。
Here is the most clear & intuitive way i know to allocate a dynamic 2d array in C++. Templated in this example covers all cases.
描述数组数组的另一个答案是正确的。
但是,如果您计划使用数组进行任何数学运算 - 或者需要一些特殊的东西(例如稀疏矩阵),您应该查看许多数学库之一,例如 TNT 在重新发明太多轮子之前
The other answer describing arrays of arrays are correct.
BUT if you are planning of doing a anything mathematical with the arrays - or need something special like sparse matrices you should look at one of the many maths libs like TNT before re-inventing too many wheels
如果您不需要任何数学运算符,我有这个网格类可以用作简单的矩阵。
I have this grid class that can be used as a simple matrix if you don't need any mathematical operators.
使用双指针是迄今为止执行速度/优化和易读性之间的最佳折衷方案。使用单个数组来存储矩阵的内容实际上就是双指针的作用。
我已经成功使用了以下模板化创建器函数(是的,我知道我使用旧的 C 风格指针引用,但它确实使调用方的代码在更改参数方面更加清晰 - 我喜欢指针,而这是不可能的您会明白我的意思):
要取消分配使用上述实用程序创建的内存,只需反向取消分配即可。
使用上述模板函数非常简单(例如):
Using the double-pointer is by far the best compromise between execution speed/optimisation and legibility. Using a single array to store matrix' contents is actually what a double-pointer does.
I have successfully used the following templated creator function (yes, I know I use old C-style pointer referencing, but it does make code more clear on the calling side with regards to changing parameters - something I like about pointers which is not possible with references. You will see what I mean):
To de-allocate the memory created using the abovementioned utility, one simply has to de-allocate in reverse.
To use these abovementioned template functions is then very easy (e.g.):