如何将巨大的矩阵传递给函数 [英] How to pass a huge matrix to a function
问题描述
我将文件中的数据读入4维数组。这些数据的大小为5MB(后来可能高达500MB)。我想将矩阵传递给
子程序。
什么是cleverst(以及初学者的简易)方法?
我在网上和书中读到了一些关于指针的内容,但是示例确实只有一维数组才会显示
。
问候
Bernhard
Hi,
I read data from a file into a 4 dimensional array. The data has a size
of 5MB (could later be up to 500MB). I want to pass the matrix to
subroutines.
What is the cleverst (and for a beginner easies) way to do so?
I read a bit about pointers on the web and in books but the examples do
only cover one-dimensional arrays.
Regards
Bernhard
推荐答案
9月19日下午2:25,Bernhard Reinhardt
< wirdseltengele ... @ freisingnet.dewrote:
On Sep 19, 2:25 pm, Bernhard Reinhardt
<wirdseltengele...@freisingnet.dewrote:
我将文件中的数据读入4维数组。这些数据的大小为5MB(后来可能高达500MB)。我想将矩阵传递给
子程序。
什么是cleverst(以及初学者的简易)方法?
我在网上和书中读到了一些关于指针的内容,但是示例确实只有一维数组才会显示
。
问候
Bernhard
Hi,
I read data from a file into a 4 dimensional array. The data has a size
of 5MB (could later be up to 500MB). I want to pass the matrix to
subroutines.
What is the cleverst (and for a beginner easies) way to do so?
I read a bit about pointers on the web and in books but the examples do
only cover one-dimensional arrays.
Regards
Bernhard
在你的情况下,我会使用一个stl容器。例如,一个指针将
指向一个矢量容器...
In your case I would use a stl container. By example a pointer points
to a vector container...
Bernhard Reinhardt写道:
Bernhard Reinhardt wrote:
我将文件中的数据读入4维数组。这些数据的大小为5MB(后来可能高达500MB)。我想将矩阵传递给
子程序。
什么是cleverst(以及初学者的简易)方法?
I read data from a file into a 4 dimensional array. The data has a
size of 5MB (could later be up to 500MB). I want to pass the matrix to
subroutines.
What is the cleverst (and for a beginner easies) way to do so?
将它包装到类/结构中并通过引用传递它。
Wrap it into a class/struct and pass it by reference.
我读了一下关于网上和书中的指针,但实例
只涵盖一维数组。
I read a bit about pointers on the web and in books but the examples
do only cover one-dimensional arrays.
一维数组很容易衰减到指针,因此在这种情况下谈论一维数组是不值得的。就例子而言,二维的
及以上更好。但话说回来,
将它们包装在一个结构中可能仍然是最简单的。如果您的
数组是动态的,那么您可以为一个处理
分配/解除分配的类更好,并具有元素访问功能
已实施。在网络上搜索数组模板或者什么。
V
-
请在通过电子邮件回复时删除资金''A' br />
我没有回复最热门的回复,请不要问
One-dimensional arrays decay to pointers so easily that it''s not worth
talking about one-dimensional array in such case. Two-dimensional ones
and up are better as far as examples are concerned. But then again,
wrapping them in a struct is probably still the simplest. And if your
array is dynamic, you''re much better off with a class that handles
allocation/deallocation for you, and has element access functionality
implemented. Search the web for "Array template" or something.
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
g。******* @ gmail.com 写道:
9月19日,2 :25 pm,Bernhard Reinhardt
< wirdseltengele ... @ freisingnet.dewrote:
On Sep 19, 2:25 pm, Bernhard Reinhardt
<wirdseltengele...@freisingnet.dewrote:
>>
我将文件中的数据读入4维数组。数据大小为5MB(以后可能高达500MB)。我想把矩阵传递给
子程序。
什么是cleverst(以及初学者的简易)方法呢?
我读了一下网上和书中的指针,但示例只包含一维数组。
Bernard
>>Hi,
I read data from a file into a 4 dimensional array. The data has a size
of 5MB (could later be up to 500MB). I want to pass the matrix to
subroutines.
What is the cleverst (and for a beginner easies) way to do so?
I read a bit about pointers on the web and in books but the examples do
only cover one-dimensional arrays.
Regards
Bernhard
在你的情况下,我会使用一个stl容器。举例来说,一个指针将
指向一个向量容器...
In your case I would use a stl container. By example a pointer points
to a vector container...
好吧,看来我的方式正确。我已经将我的数组[] [] [] []转换为
一个向量<矢量<矢量< vector< double
我部分使用向量部分使用普通数组
,因为我不知道如何处理一个独特的元素
矢量形式的矩阵。
我可以做
vector< doublea;
cout<< a [0];
但
vector< vector< double> b;
cout<< b [0] [0];
不会编译。
下一个问题:
是可以将一个向量传递给一个函数,使得
函数可以处理原始数据并且不会制作本地副本吗?
In在后期阶段,我的矩阵可能有10个维度。所以它将成为一个
向量<矢量<矢量<矢量<矢量<矢量<矢量<向量<
向量<矢量<矢量<矢量<矢量< double
对我来说似乎不太方便。
问候
Bernhard
Ok it seems I′m on the right way. I already convert my array[][][][] to
a vector < vector < vector < vector <double
I′m partly working with the vector an partly with the normal array
because I don′t know how to address a unique element of the matrix in
vector form.
I can do
vector <doublea;
cout<<a[0];
but
vector <vector <double>b;
cout<<b[0][0];
wont compile.
Next question:
Is it possible to pass a vector to a function in such manner that the
function will work on the original data and will not make a local copy?
In a later stage my matrix may have 10 dimensions. So it will become a
vector < vector < vector < vector < vector < vector < vector < vector <
vector < vector < vector < vector < vector <double
that seems not quite handy to me.
Regards
Bernhard
这篇关于如何将巨大的矩阵传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!