如何用N个元素创建N维数组? [英] How to create an N dimensional array with N elements?

查看:172
本文介绍了如何用N个元素创建N维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在谷歌搜索并阅读我的书籍,但我还没想出一个

解决方案(更不用说优雅的解决方案)来创建一个多维数组,其中包含

a运行时确定的维数。我还查看了

boost :: multi_array.hpp和Giovanni Bavistrelli的Array代码。这两个似乎都不允许动态数组维度。


例如,用户选择一个二维数组,我想创建:

MyObject ** array = new MyObject [dim1_size] [dim2_size];


如果他们选择4 dim数组:

MyObject ** ** array = new

MyObject [dim1_size] [dim2_size] [dim3_size] [dim4_size];


除了使用bigugly if语句外,我该怎么做?


谢谢,

布莱恩

Ive been googling and reading through my books but I haven''t figured out a
solution (much less an elegant one) to create a multidimensional array with
a runtime determined number of dimensions. I also checked out the
boost::multi_array.hpp, and Giovanni Bavistrelli''s Array code. Neither of
these seem to allow dynamic array dimensions.

For example, the user selects a 2 dimensional array, I want to create:
MyObject** array = new MyObject[dim1_size][dim2_size];

And if they select a 4 dim array:
MyObject**** array = new
MyObject[dim1_size][dim2_size][dim3_size][dim4_size];

Besides using bigugly if statements, how do I do this?

Thanks,
Bryan

推荐答案

经过进一步反思,我认为创建一个n维数组可能是

除了这一点...


我想要做的是,在一天结束时,是拿一个对象列表

(虽然我们暂时说它们是整数)并将它乘以

本身N次。

因此,如果我有一个包含10个元素的数组,我需要创建一个新的列表

,其中包含100个元素,以及po将这100个元素与结果联系起来

of:

[0] [0] * [0] [0]

[0] [ 0] * [0] [1]

等等。


3个dims,列表长度为10000个元素,依此类推。


如果这是一个嵌套的for循环,有N个嵌套,这将是简单的......但是不知道有多少循环可以使用我不是

肯定如何解决这个问题。


有什么建议吗?

谢谢

B
Upon further reflection, I think creating an n dimensional array may be
besides the point...

What I am trying to do, at the end of the day, is to take a list of objects
(lets just say they are ints for the moment though) and multiply it by
itself N number of times.

So if I have an array with 10 elements in it, I need to create a new list
with 100 elements in it, and populate those 100 elements with the results
of:
[0][0] * [0][0]
[0][0] * [0][1]
etc. etc.

With 3 dims, the list would be 10000 elements long, and so on.

If this was a nested for loop, with N number of nests this would be
simple... but without knowing ahead of time how many loops to use Im not
sure how to go about this.

Any suggestions?
Thanks
B




" BCC" <峰; br *** @ akanta.com>在消息中写道

news:zI ***************** @ newssvr25.news.prodigy.co m ...

"BCC" <br***@akanta.com> wrote in message
news:zI*****************@newssvr25.news.prodigy.co m...
经过进一步的反思,我认为创建一个n维数组可能会更有意思......

在一天结束时,我想要做的是采取一个
对象的列表(尽管我们暂时说它们是整数)然后将它本身乘以N次。

所以如果我有一个包含10个元素的数组在其中,我需要创建一个包含100个元素的新列表,并用结果填充这100个元素:
[0] [0] * [0] [0 ]
[0] [0] * [0] [1]
等等。

3个dims,列表长度为10000个元素,依此类推。

如果这是一个嵌套的for循环,有N个嵌套,这将是简单的...但不知道提前使用了多少循环我不是
肯定如何解决这个问题。

有什么建议吗?
Upon further reflection, I think creating an n dimensional array may be
besides the point...

What I am trying to do, at the end of the day, is to take a list of objects (lets just say they are ints for the moment though) and multiply it by
itself N number of times.

So if I have an array with 10 elements in it, I need to create a new list
with 100 elements in it, and populate those 100 elements with the results
of:
[0][0] * [0][0]
[0][0] * [0][1]
etc. etc.

With 3 dims, the list would be 10000 elements long, and so on.

If this was a nested for loop, with N number of nests this would be
simple... but without knowing ahead of time how many loops to use Im not
sure how to go about this.

Any suggestions?




递归。


不完全确定这是你要求的,但希望将足够接近你的b
$ b #include< algorithm>

#include< iterator>

#include< iostream>

#include< list>

using namespace std;


void generate(std :: list< int>& res,std :: list< int>常量和放大器; l,int depth,int

value = 1)

{

if(depth == 0)

{

res.push_back(value);

}

else

{

for(std :: list< int> :: const_iterator i = l.begin(); i!= l.end(); ++ i)

generate(res,l,depth - 1,价值*(* i));

}

}


int main()

{

//设置起始列表

std :: list< int> l;

l.push_back(1);

l.push_back(2);

l.push_back(3);

//生成结果列表

std :: list< int> res;

generate(res,l,4);

//打印结果

std :: copy(res.begin(), res.end(),std :: ostream_iterator< int>(std :: cout,"

"));

}

john



Recursion.

Not completely sure this is what you are asking for but hopefully will be
close enough for you to adapt.

#include <algorithm>
#include <iterator>
#include <iostream>
#include <list>
using namespace std;

void generate(std::list<int>& res, std::list<int> const& l, int depth, int
value = 1)
{
if (depth == 0)
{
res.push_back(value);
}
else
{
for (std::list<int>::const_iterator i = l.begin(); i != l.end(); ++i)
generate(res, l, depth - 1, value*(*i));
}
}

int main()
{
// setup starting list
std::list<int> l;
l.push_back(1);
l.push_back(2);
l.push_back(3);
// generate result list
std::list<int> res;
generate(res, l, 4);
// print results
std::copy(res.begin(), res.end(), std::ostream_iterator<int>(std::cout, "
"));
}

john


BCC写道:
经过进一步反思,我认为创建一个n维数组可能是
关键点......

我想在一天结束时做的就是拿一份物品清单
(尽管我们暂时说它们是整齐的)并且将它本身乘以N次。

所以如果我有一个包含10个元素的数组,我需要创建一个包含100个元素的新列表
,并用结果填充这100个元素:
[0] [0] * [0] [0]
[0] [0] * [0] [1] <等等。使用3个dims,列表长度为10000个元素,依此类推。

如果这是一个嵌套的for循环,带有N个数字巢穴这将是简单的... b ut提前知道要使用多少循环我不确定如何去做这个。

有什么建议吗?
Upon further reflection, I think creating an n dimensional array may be
besides the point...

What I am trying to do, at the end of the day, is to take a list of objects
(lets just say they are ints for the moment though) and multiply it by
itself N number of times.

So if I have an array with 10 elements in it, I need to create a new list
with 100 elements in it, and populate those 100 elements with the results
of:
[0][0] * [0][0]
[0][0] * [0][1]
etc. etc.

With 3 dims, the list would be 10000 elements long, and so on.

If this was a nested for loop, with N number of nests this would be
simple... but without knowing ahead of time how many loops to use Im not
sure how to go about this.

Any suggestions?




递归。



Recursion.


这篇关于如何用N个元素创建N维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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