1. ## Multidimential(3) vectors

How do I define a 3 dimension vector?

Code:
```#ifndef FIELD_H_INCLUDED
#define FIELD_H_INCLUDED

#include <memory>
#include <vector>
#include "object.h"

class field{
public:
int length, width, height;
vector<shared_ptr<object> > location; // 3 dim vector ???
};

#endif // FIELD_H_INCLUDED```

2. vector< vector< vector< shared_ptr< object > > > >

3. Yeah... I did it like this instead:
Code:
```#ifndef FIELD_H_INCLUDED
#define FIELD_H_INCLUDED

#include "object.h"

class field{
public:
int length, width, height;
object ****location;

field(int l = 1, int w = 1, int h = 1);
field(const field&);

~field();
};

#endif // FIELD_H_INCLUDED```
Code:
```#include "field.h"

field::field(int l, int w, int h) : length(l), width(w), height(h)
{
int i, j, k;
location = new object*** [length];
for(i = 0;i < length;i++)
{
location[i] = new object** [width];
for(j = 0;j < width;j++)
{
location[i][j] = new object* [height];
for(k = 0;k < height;k++)
location[i][j][k] = 0;
}
}
}

field::field(const field& f) : length(f.length), width(f.width), height(f.height)
{
int i, j, k;
location = new object*** [length];
for(i = 0;i < length;i++)
{
location[i] = new object** [width];
for(j = 0;j < width;j++)
{
location[i][j] = new object* [height];
for(k = 0;k < height;k++)
location[i][j][k] = f.location[i][j][k];
}
}
}

field::~field()
{
int i, j, k;
for(i = 0;i < length;i++)
{
for(j = 0;j < width;j++)
{
for(k = 0;k < height;k++)
if(location[i][j][k] != 0)
delete[] location[i][j][k];
delete[] location[i][j];
}
delete[] location[i];
}
delete[] location;
}```
I realized I don't need vectors anyway. I won't have any reason to resize after defining and allocating it, so I guess I don't need all the extra bloat.

Thanks for the example though.

4. Then I suggest std::array. Or even better, there should be some boost containers that simplify this (boost::multi_array).

5. Simplify?! How much simpler could it get? I know it's like O(h * w * l) I think, but I can live with that since it only gets called once per field, field being a very long lasting class. It doesn't get created/destroyed often.

Code:
`boost::multi_array<double, 3> A(boost::extents[3][4][2]);`
Done.

7. One approach that may be feasible is to have a single std::vector<std::shared_ptr<object>> that holds length * width * height elements. Then, you simulate the 3D array by having a std::vector<std::shared_ptr<object>*> that holds length * width elements. Each of these pointers point to shared_ptrs from the earlier vector, at intervals of height units. Finally, you define a std::vector<std::shared_ptr<object>**>, say named location, with length elements. Each of these pointers point to the pointers to shared_ptrs from the previous vector, at intervals of width units. Now, if I did not mess up in my description , you should be able to access the original shared_ptrs by writing location[x][y][z]. You would have effectively only required three allocations rather than the many that you have now.

I believe that Boost.MultiArray uses an approach similiar to this, but of course it requires Boost.

8. I would not allocate a fixed-size 3-dimensional array like that. There's no need to width*height+1 calls to new when it can be done with just 3, and then the freeing can be done with just 3 calls to delete and no loops. Using vectors you could have an item vector, a row vector, and a column vector. The row vector contains pointers into the column vector which contains pointers into the item vector.

9. .....or you can jam the data in a structure and use one linear array of those structures to represent all the data in a much simpler fashion.

10. It has to be 3 dimensional because it is used to represent space.

11. Ok....and?

Code:
```struct Vector3
{
float x;
float y;
float z;

Vector3() : x(0.0f),y(0.0f),z(0.0f)
{
}

Vector3(float x,float y,float z) : x(x),y(y),z(z)
{
}
};```
There is no need to represent 'space' as an array since that makes 'space' into a finite grid. Rather use Vector3 to represent any point in 3 space.

Code:
```std::vector<Vector3> space;

Vector3 vec;
for (unsigned int i = 0;i < 10; ++i)
{
vec.x = GetRandomFloatRange(-5000.0f,5000.0f);
vec.y = GetRandomFloatRange(-5000.0f,5000.0f);
vec.z = GetRandomFloatRange(-5000.0f,5000.0f);
space.push_back(vec);
};

float GetRandomFloatRange(float min,float max)
{
unsigned int temp = rand() % 1000;
float coef = temp / 1000.0f;

return min + coef * (max - min);
}```
All 3D games work in 3 space or 4 space (x,y,z,w) and they do not require huge multi-dimensional arrays. I avoid multi-dimensional arrays and multi-dimensional vectors at all costs. They can be quite confusing past 2 dimensions and the memory required plus the minor issue of speed when accessing multi-dimensional arrays keeps me far away from them.

This code could be used to represent a starfield centered around the origin. If you move the points along a vector through local space and wrap them at the edges and then transform this 'system' to world space you can simulate a starfield around a player or object. Just because you have 3 coordinates or 3 components of a vector representing 3 space that does not mean you need a 3 dimensional array to represent it in memory. An array does not represent 'space' but it represents space broken up into finite grid cells.