Jump to content
  • Advertisement
Sign in to follow this  
pc31

tab with vector or list from STL

This topic is 4873 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want a grid with voxels (a tab with 3 dimentions) In each voxel, I want a vector from stl. (in each vector i will add integer) But it dont work, i dont succed to do it vector<int> m_VectorGrid[16][16][16]; (m_VectorGrid[x][y][z]).push_back(1); : my program block vector < vector < vector < vector < int > > > > m_VectorGrid; : I dont know how to use it please help me :| [Edited by - pc31 on June 18, 2005 11:30:53 AM]

Share this post


Link to post
Share on other sites
Advertisement
somebody advice me to use list


double *d;
d = new (double)(5);
d[1]=10.0;
this exemple work with double but not with stl::list


i try :

list<int> *l;
l = new (list<int>)(5);
l[1].push_back(1);

my program block!!!!!!

:(

Share this post


Link to post
Share on other sites
If by "my program block" you mean it compiles but then crashes, your problem is that you're using an index of one when you should be using an index of zero (or no index at all). Change:

l[1].push_back(1);

To:

l[0].push_back(1);

Or better yet:

l->push_back(1);

If you're getting compiler errors, please copy and paste (DON'T paraphrase) your error message and code.

Share this post


Link to post
Share on other sites
whats wrong with

class Integer_Grid
{
vector<int> grid;

public:
Integer_Grid() : grid(16*16*16) { }
push_back(unsigned char x, unsigned char y, unsigned char z, int i) { grid[x*y*z].push_back(i); }
};

the point being the 'grid[x*y*z].push_back()' part, just multiple numbers to access the right element, its better than using nested vectors.

Share this post


Link to post
Share on other sites
Quote:
Original post by dmatter
whats wrong with

class Integer_Grid
{
vector<int> grid;

public:
Integer_Grid() : grid(16*16*16) { }
push_back(unsigned char x, unsigned char y, unsigned char z, int i) { grid[x*y*z].push_back(i); }
};

the point being the 'grid[x*y*z].push_back()' part, just multiple numbers to access the right element, its better than using nested vectors.


Nothing, apart from you're calculating the array index incorrectly.

It should be:

x + y * 16 + z * 256.

Share this post


Link to post
Share on other sites
Multiplying the index like dmatter said is usually easier, only you should use x+num_x*(y+num_y*z) where num_x is the size in the x direction (16 in dmatter's example).
Also, instead of directly creating an array of vectors it is probably a good idea to create an array of structs or classes instead. If you later decide to add something else to your voxels it will be a lot easier.

Share this post


Link to post
Share on other sites
Quote:
Original post by python_regious
Quote:
Original post by dmatter
whats wrong with

class Integer_Grid
{
vector<int> grid;

public:
Integer_Grid() : grid(16*16*16) { }
push_back(unsigned char x, unsigned char y, unsigned char z, int i) { grid[x*y*z].push_back(i); }
};

the point being the 'grid[x*y*z].push_back()' part, just multiple numbers to access the right element, its better than using nested vectors.


Nothing, apart from you're calculating the array index incorrectly.

It should be:

x + y * 16 + z * 256.



oops yes sorry for that (its too hot [flaming]) but there shouldn't be a problem with multiplying indicies and std::vector would proably operate faster too this way - so its all for the best.

Wrap it in a function (inline it if its a member function) so that you don't need to worry about forgetting to offset the indicies by the correct values (not that an experienced programmer -such as myself- would ever make a simple mistake like that [grin])

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!