简体   繁体   English

3D 向量与 3D 不同大小数组的速度

[英]Speeds of 3D Vector Versus 3D Array of Varying Size

I'm designing a dynamic hurtbox for characters in a text-based game, which catches the locations of hits (or misses) of a weapon swung at them.我正在为基于文本的游戏中的角色设计一个动态伤害框,它可以捕捉武器向他们挥动的命中(或未命中)的位置。 The location (indices) and damage (magnitude) of hits are then translated into decreases in corresponding limb health variables for a character.然后将命中的位置(指数)和伤害(大小)转化为角色相应肢体健康变量的减少。 My thoughts are that this hurtbox would best be implemented using a class with some 3D vector/array member.我的想法是最好使用带有一些 3D 向量/数组成员的 class 来实现这个伤害箱。

Naturally, I might want varying dimensions of the 3D container for different sizes of enemy, but I'm aware that size is usually determined upon initialization.当然,对于不同大小的敌人,我可能希望 3D 容器的不同尺寸,但我知道尺寸通常在初始化时确定。 So here's my question:所以这是我的问题:

Would it be more efficient to use a C-style dynamic array, the size of which I can decide and allocate inside a parameterized constructor, like so?使用 C 风格的动态数组会更有效吗,我可以在参数化构造函数中决定和分配它的大小,就像这样?

class hurtBox {
 private:
   int ***hurtBoxMatrix;
 public:
   hurtBox(int l, int w, int h) {
     hurtBoxMatrix = new int**[l];
     for (int i = 0; i < l; i++) {
       hurtBoxMatrix[i] = new int*[w];
       for (int j = 0; j < w; j++) {
         hurtBoxMatrix[i][j] = new int[h] ();
       }
     }
   }
};

Or, would a vector that I push elements into, up to my desired dimensions, suffice?或者,我将元素推入到我想要的尺寸的向量是否足够?

class hurtBox {
 private:
   vector<vector<vector<int>>> hurtBoxMatrix;
 public:
   hurtBox(int l, int w, int h) {
     for (int i = 0; i < l; i++) {
       hurtBoxMatrix.push_back(vector<vector<int>>);
       for (int j = 0; j < w; j++) {
         hurtBoxMatrix[i].push_back(vector<int>);
         for (int k = 0; k < h; k++) {
           hurtBoxMatrix[i][j].push_back(0);
         }
       }
     } 
   }
};

I imagine the former, since that first allocation is constant time, right?我想是前者,因为第一次分配是恒定的时间,对吧? Is there a way to do this that's better than either of these?有没有比这两种方法更好的方法?

Thanks in advance.提前致谢。

You'd be better off simply allocating the 3D array in a single allocation, and use indexing to access the elements.您最好在一次分配中简单地分配 3D 数组,并使用索引来访问元素。 Allocation for the std::vector storage can then be handled in the constructor for std::vector.然后可以在 std::vector 的构造函数中处理 std::vector 存储的分配。

In general it's best to avoid:一般来说,最好避免:

  1. multiple allocations多重分配
  2. repeatedly calling push_back反复调用 push_back
class hurtBox {
 private:
   vector<int> hurtBoxMatrix;
   int m_l;
   int m_w;
   int m_h;
 public:
   hurtBox(int l, int w, int h) 
      : hurtBoxMatrix(l * w * h), m_l(l), m_w(w), m_h(h) {}

   int& operator (int i, int j, int k) {
     return hurtBoxMatrix[ I*m_w*m_h + j*m_w + k ];
   }
   const int operator (int i, int j, int k) const {
     return hurtBoxMatrix[ i*m_w*m_h + j*m_w + k ];
   }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM