In article <(E-Mail Removed). com>,

"Guardiano del Faro" <(E-Mail Removed)> wrote:

> Hello!!

>

> i'm just a dummie in C/C++ and i need you help for solving my little

> problem.

>

> I have to create a 3D grid of points. Each point has the 3 coordinates

> and a boolean value. (I made a structure in order to make this).

>

> My task is to scan the grid through the x, y and z planes.

>

> According to you, which is the best way to create this data structure?

>

> I have tried to create a vector of vectors of vectors of point

> structure, so to access to each point coordinate i just have to do

> like:

>

> Grid[i][j][k].x

>

> So the i index will give me all the points which are on the x

> planes....

>

> but i have now idea about how to build it!!!

> Does anybody has any clue?!
My suggestion:

class Grid3D {

std::vector<bool> rep;

unsigned h, w, d;

public:

typedef std::vector<bool>::reference reference;

typedef std::vector<bool>::const_reference const_reference;

Grid3D( unsigned x, unsigned y, unsigned z ):

h( x ), w( y ), d( z ), rep( x * y * z )

{ }

reference at( unsigned x, unsigned y, unsigned z ) {

assert( x < h && y < w && z < d );

return rep[ x*w*d + y*d + z ] != 0;

}

const_reference at( unsigned x, unsigned y, unsigned z ) const {

assert( x < h && y < w && z < d );

return rep[ x*w*d + y*d + z ] != 0;

}

// other functions to taste

};

use like this:

int main() {

Grid3D g( 10, 10, 10 );

g.at( 3, 4, 6 ) = true;

if ( g.at( 7, 5, 9 ) ) {

cout << "should not print";

}

}

Caution: I wrote the above with a puppy in my lap, so there may be some

errors.