分配四维动态阵列...... [英] Allocating Four Dimensional Dynamic Arrays...

查看:58
本文介绍了分配四维动态阵列......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我该如何分配一个四维动态的
数组?


我只知道如何制作二维动态数组:

double ** v;

v = new double * [C];

for(int i = 0; i< C; i ++)< br $>
{

v [i] = new double [n];

}


好​​吧我的计划已经改变了一点,我需要制作一个数组来存储

matrices" v" (就像我上面用动态尺寸C * n分配的那个)。

我不打算使用上面的代码......我想分配所有四个

尺寸在一个片段中。它需要以类似的方式完成,因为

我不确定内存是否会连续。


我想要做的是拥有一个数组深度可变的数量,

和前一行之后的行将具有n。乘以矩阵的数量

v。


例如...如果n为3:


在第一行,将包含1个矩阵v。

在第二行,将包含3个矩阵v。

在第三行将包含9个矩阵v。


在我写的程序中,访问这些成员会很方便

像这样:


例如:

[1,2,1,2]

这与第二行和第三行的矩阵v相关

此矩阵的列。它将访问由前两个参数选择的成员v的第二行和第三列



Actually, how would I go about allocating a four-dimensional dynamic
array?

I only know how to make two dimensional dynamic arrays:
double **v;
v = new double*[C];
for (int i=0; i<C; i++)
{
v[i] = new double[n];
}

Well my plans have changed a bit and I need to make an array to store
matrices "v" (like the one I allocated above with dynamic dimensions C*n).
I''m not going to use the code above... I want to allocate all four
dimensions in one snippet. It needs to be done in a similar manner because
I''m not sure that the memory will be contiguous.

What I want to do is have an array going a variable number of levels deep,
and the row after the previous will have "n" times the amount of matrices
"v".

For example... if n was 3:

On the first row there would contain 1 matrix "v".
On the second row there would contain 3 matrix "v".
On the third row there would contain 9 matrix "v".

In the program I am writing it would be convenient to access these members
like this:

example:
[1, 2, 1, 2]
This would correlate to the matrix v found on the second row and third
column of this matrix. It would access the second row and third column of
the member v that was seleced by the first two parameters)

推荐答案



" fivelitermustang" <音响************** @ shaw.ca>在消息中写道

news:26 ****************************** @ localhost.ta lkaboutprogramming。 com ...

"fivelitermustang" <fi**************@shaw.ca> wrote in message
news:26******************************@localhost.ta lkaboutprogramming.com...
实际上,我将如何分配一个四维动态数组?

我只知道如何制作二维动态数组:
double ** v;
v = new double * [C];
for(int i = 0; i< C; i ++)
{
v [ i] = new double [n];
}

我的计划已经改变了一点,我需要制作一个数组来存储
矩阵v。 (就像我上面用动态尺寸C * n分配的那个)。
我不打算使用上面的代码...我想在一个片段中分配所有四个
维度。它需要以类似的方式完成,因为
我不确定内存是否会连续。

我想要做的是让一个数组变为可变数量的级别深,
和前一行之后的行将具有n。乘以矩阵的数量
v。

例如......如果n为3:

第一行将包含1个矩阵在第二行中将包含3个矩阵v。
在第三行上将包含9个矩阵v。

在程序我写的是方便访问这些成员
像这样:

例如:
[1,2,1,2]
这将与矩阵v在该矩阵的第二行和第三列上找到。它将访问由前两个参数选择的成员v的第二行和第三列)
Actually, how would I go about allocating a four-dimensional dynamic
array?

I only know how to make two dimensional dynamic arrays:
double **v;
v = new double*[C];
for (int i=0; i<C; i++)
{
v[i] = new double[n];
}

Well my plans have changed a bit and I need to make an array to store
matrices "v" (like the one I allocated above with dynamic dimensions C*n).
I''m not going to use the code above... I want to allocate all four
dimensions in one snippet. It needs to be done in a similar manner because
I''m not sure that the memory will be contiguous.

What I want to do is have an array going a variable number of levels deep,
and the row after the previous will have "n" times the amount of matrices
"v".

For example... if n was 3:

On the first row there would contain 1 matrix "v".
On the second row there would contain 3 matrix "v".
On the third row there would contain 9 matrix "v".
In the program I am writing it would be convenient to access these members
like this:

example:
[1, 2, 1, 2]
This would correlate to the matrix v found on the second row and third
column of this matrix. It would access the second row and third column of
the member v that was seleced by the first two parameters)



一个大问题是维度的数量是否是常量在

编译时知道,或者你是否想在运行时改变它。大部分时间

你说四个维度然后你说''可变数量级别''。


如果前者然后看多数阵列来自boost,
http://www.boost .org / libs / multi_array / doc / index.html


如果后来则不容易。一个问题是当你需要一个变量数整数来指定

元素时,如何访问

多维数组。你必须使用可变数量的

参数重载operator()。


无论如何,它听起来是一个非常笨拙的数据结构,我怀疑那你好b $ b真的需要这样做,但后来我不知道你实际上有什么问题

试图解决。


john


A big issue is whether the number of dimensions is a constant known at
compile time, or whether you want to vary that at run time. Most of the time
you say four dimensions but then you say ''a variable number of levels deep''.

If the former then look at multi_array from boost,
http://www.boost.org/libs/multi_array/doc/index.html.

If the later then its not easy. One problem is how you access the
multi-dimensional array when you need a variable number integers to specify
an element. You would have to overload operator() with a variable number of
parameters I think.

In any case it sounds a very unwieldy data structure, I doubt that you
really need to do this, but then I don''t know what problem you are actually
trying to solve.

john


级别数量由用户在运行时指定。而不是

变量,我的意思是说动态,因为当用户指定时,内存将在运行时分配。


实际上,我已经看过这个问题了,我已经为这个问题编写了我的

伪代码来处理这种类型的结构。这个

金字塔式结构将是我用的最佳选择。


我想知道这样做的语法是什么我怎么去取消分配内存等等$


The amount of levels is specified at run time by the user. Instead of
variable, I meant to say dynamic as the memory would be allocated at
run-time when the user specifies it.

Actually, I''ve looked at the problem and I''ve already coded out my
pseudo-code for this problem to work with this type of strucutre. This
"pyramid"-like structure would be the best option to use in my case.

I was wondering what the syntax would be to make this and how would I go
about deallocating the memory and so forth.




" fivelitermustang" <音响************** @ shaw.ca>在消息中写道

news:ce ****************************** @ localhost.ta lkaboutprogramming。 com ...

"fivelitermustang" <fi**************@shaw.ca> wrote in message
news:ce******************************@localhost.ta lkaboutprogramming.com...
用户在运行时指定级别数量。而不是变量,我的意思是说动态,因为当用户指定时,将在运行时分配内存。

实际上,我已经查看了问题,我已经为这个问题编写了我的
伪代码来处理这种类型的结构。这种类似金字塔的结构在我的情况下是最好的选择。

我想知道这样做的语法是什么,我将如何进行
关于释放内存等等。
The amount of levels is specified at run time by the user. Instead of
variable, I meant to say dynamic as the memory would be allocated at
run-time when the user specifies it.

Actually, I''ve looked at the problem and I''ve already coded out my
pseudo-code for this problem to work with this type of strucutre. This
"pyramid"-like structure would be the best option to use in my case.

I was wondering what the syntax would be to make this and how would I go
about deallocating the memory and so forth.




我不确定你的要求是什么,也许你给我看了

伪代码。


但无论如何答案是通常的答案,你写一个类定义

你需要的界面然后你去做编写实现。

大问题似乎是你需要在

构造函数和某种元素访问方法中使用可变数量的参数。


class MultiArray

{

public:

MultiArray(int rank,...);

双&安培; operator()(int first_index,...);

};


rank是数组的维数,预计是

后跟每个维度的大小(也是int')。期望operator()

用每个维度的int调用来访问单个元素。


注意这里没有安全性,没有对于C ++的方式,或者你检查

是否使用正确数量的

参数调用构造函数或operator()。这就是为什么很少这样做的原因。

要访问可变数量的参数,你需要使用

< stdarg.h>中的宏;。这是构造函数可能看起来像什么的片段


#include< stdarg.h>


MultiArray :: MultiArray(int排名,...):my_rank(排名)

{

va_list arg_ptr;

va_begin(arg_ptr,rank);


//计算总大小

int size = 1;

for(int i = 0; i< rank; ++ i)

size * = va_arg(arg_ptr,int);

...

va_end(arg_ptr);

}


这只是为了给你一个想法。我认为你有很多工作要做

来完成这项工作。


john



I''m not sure exactly what you requirements are, maybe if you showed me the
pseudo code.

But in any case the answer is the usual one, you write a class that defines
the interface you need and then you go about writing the implementation. The
big issue seems to be that you require variable numbers of arguments in the
constructor and in some sort of element access method.

class MultiArray
{
public:
MultiArray(int rank, ...);
double& operator()(int first_index, ...);
};

rank is the number of dimensions of your array, and it is expected to be
followed by the size of each dimension (also int''s). operator() is expected
to be called with an int for each dimension to access an individual element.

Note there is no safety here, there is no way for C++ or you to check that
the constructor or the operator() is called with the correct number of
arguments. This is why this sort of thing is rarely done.

To access the variable number of parameter you need to use the macros in
<stdarg.h>. Here''s a snippet of what the constructor might look like

#include <stdarg.h>

MultiArray::MultiArray(int rank, ...) : my_rank(rank)
{
va_list arg_ptr;
va_begin(arg_ptr, rank);

// calculate total size
int size = 1;
for (int i = 0; i < rank; ++i)
size *= va_arg(arg_ptr, int);
...
va_end(arg_ptr);
}

That''s just to give you an idea. I think you have quite a lot of work to do
to make this work.

john


这篇关于分配四维动态阵列......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆