I have to do a generic class Grid3D that implements a 3-dimensional structure of cells. Each cell can be accessed by giving the coordinates of the plane (z), row (y) and column (x).
I'll be able to request and write the value of a cell to
position (z, y, x) through operator (). Ex: G (3,1,6) = G (3,2,2).
I think that i have to do a multi-dimensional array (array of array of array) but i don't know how to do it. I can't use vector, is a specific request.
I would suggest making a wrapper class, as is mentioned in the article hamsterman gave. Store it as a 1d array using the equation: index=z*width*height+y*width+x
Ok I have a problem, I have my 3D array int 3dArray[xSize][ySize][zSize];
how can i refer to it with a pointer?
I know that for 1D array i can do like this
1 2 3 4
int array[3]={-1,-2,-3};
int *p;
p=array;
p++; // array[1]
I know that for 2D array i can do like this
1 2 3 4 5
int array[2][3]={{1,2,3},{4,5,6}};
int (*p)[3]; // Pointer to an array of 3 elements (int)
p=array;
p[0][1]=0;
p++; // it moves to the second rows of matrix
Ok, I do it.
I have implemented the code you posted above and i've implemented some other methods.
Now I have to implement a generic function evaluate that recive as parameter a grid3d A and a property P, and make as result a grid3d B of boolean where B(x,y,z)=true if P(A(x,y,z)) is vefify (is true). for example:
- I have a grid3d of int and i want to verify which cell have a number in a range ( or if the number is int)
- I have a grid3d of couple name-surname, and i want to verify witch cell contain a specific surname (or witch cell contain a surname that start with a specific letter)
I suppose that i should use functor for do this. But i don't know how.
Or maybe there is an another method to do this.
template <typename T, typename F>
void evaluate(const Grid3D<T> &a,const F &pred){
int sz_x = a.sz_x;
int sz_y = a.sz_y;
int sz_z = a.sz_z;
Grid3D<bool> b(sz_x,sz_y,sz_z,false); //costruttore secondario
for(typename Grid3D<T>::size_type i = 0; i < a.size(); ++i){
if(pred(a[i]))
b[i] = true;
}
}
as a global function
and then in the main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
struct predicate{ // say if a number is in a range min-max
int min;
int max;
predicate() : min(0), max(5) {}
booloperator()(int v) const {
return ((v > min) & (v < max));
}
};
predicate pred;
Grid3D<int> a(2,1,1);
evaluate(a,pred);
but i have an error
In function `int main(int, char**)':
no matching function for call to `evaluate(Grid3D<int>&, main(int, char**)::predicate&)'