创建`char *** data`? [英] Creating `char ***data`?
问题描述
我必须创建一个三维数组,在对象创建时分配。我做了这样的东西之前与正常数组。
typedef unsigned char byte; // = |
byte *** data;
如果您使用 C ++
,我强烈建议您使用 std :: vector
代替原始数组。
类似的东西:
std :: vector< std :: vector< std :: vector< char> > >数据(3,std :: vector< std :: vector< char>(3,std :: vector>(3,0)))
将创建一个3x3x3数组 char
,所有初始化为 0
。
然后可以使用 char ***
:
data [0] [0] [0] = 1;
根据您的需要, std :: vector
并将您的三维空间嵌入其中。这将紧固计算和复制操作,因为值将在一个连续的内存块中。
您可以根据需要内联值的方式,但是这里是一个例子:
对于这样的三维数组:
a ---- b
| \ | \
| d ---- c
e- | --f |
\ | \ |
h ---- g
您可以在内存中存储如下值: p>
a,b,c,d,e,f,g,h
pre>
这需要一些数学,但是如果你封装了这个,你甚至可以实现
向量的相同级别的可用性
向量向量,具有更高的性能。
I have to create a 3-Dimensional array, wich gets allocated at object creation. I've done such stuff before with normal arrays.
typedef unsigned char byte; // =| byte ***data;
解决方案If you're using
C++
, I strongly advise you usingstd::vector
instead of raw arrays.Something like:
std::vector<std::vector<std::vector<char> > > data(3, std::vector<std::vector<char> >(3, std::vector<char>(3, 0)));
Will create a 3x3x3 array of
char
s, all initialized to0
.You can then access the items the same way you would with a
char***
:data[0][0][0] = 1;
Depending on your needs you might also use only one
std::vector
and inline your three-dimensional space into it. This would fasten both computation and copy operations, as the values would then be in a contiguous block of memory.The way you could inline the values depending on your needs, but here is an example:
For a 3 dimensional array like this:
a----b |\ |\ | d----c e-|--f | \| \| h----g
You might store values like this in memory:
a, b, c, d, e, f, g, h
This require some maths, but if you encapsulate this, you can even achieve the same level of usability of a
vector
ofvector
ofvector
, with much higher performances.这篇关于创建`char *** data`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!