创建`char *** data`? [英] Creating `char ***data`?

查看:199
本文介绍了创建`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 using std::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 chars, all initialized to 0.

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 of vector of vector, with much higher performances.

这篇关于创建`char *** data`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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