how to create a 3d address array which pointed to a single std::vector?

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

Recommended Posts

How to create a 3d address array which pointed to a single std::vector? Just something like the following.... //create this address array //if i only want coordinate (2,3,3) to store value , it CREATE and POINT to a std::vector and input data to it //but other coordinates do not point and create any vector // then i use a for loop to get all the data for (int x=0;x<=3;x++) for (int y=0;y<=3;y++) for (int z=0;z<=3;z++) { if (cubes[x][y][z]!=NULL) { //get the address from it and point to the std::vector i wanted. } } //After this, i also want coordinate (2,2,2) to to store value , it CREATE and POINT to another new std::vector // so now i hv two vectors for two address. // then use the above for loop to get all the values again. Thx.

Share on other sites
std::vector<T> *cubes[4][4][4] = {0};

Share on other sites
Quote:
 Original post by ToohrVykstd::vector *cubes[4][4][4] = {0};

T is??
and how can i create a new vector by this?

Share on other sites
Quote:
 Original post by gohkgohkT is??

Well, the type inside the vector.

Quote:
 and how can i create a new vector by this?

cubes[x][y][z] = new std::vector<T>;

Don't forget to clean up afterwards.

Share on other sites
Quote:
Original post by ToohrVyk
Quote:
 Original post by gohkgohkT is??

Well, the type inside the vector.

Quote:
 and how can i create a new vector by this?

cubes[x][y][z] = new std::vector<T>;

Don't forget to clean up afterwards.

Thx

[Edited by - gohkgohk on March 24, 2008 5:22:44 PM]

Share on other sites
std::vector<float> *cubes[16][16][16];

cubes[2][2][2] = new std::vector<float>;
cubes[3][2][1] = new std::vector<float>;
cubes[2][1][1] = new std::vector<float>;
cubes[4][1][1] = new std::vector<float>;
cubes[5][1][1] = new std::vector<float>;
cubes[6][1][1] = new std::vector<float>;

cubes[6][1][1]->push_back(1233.0f);
cubes[2][1][1]->push_back(123.0f);
cubes[3][2][1]->push_back(122.0f);
cubes[4][1][1]->push_back(121.0f);

do it really add value into the vector?
and how can i detect the cubes[3][4][2] exist or not?
and how can i get the value stored at vector of cubes[6][1][1]?
thx

Share on other sites
First, when making an array of pointers, you might want to zero-initialize:
std::vector<float> *cubes[16][16][16] = {0};

Second, are you sure you want a 16 by 16 by 16 cube of std::vectors?

Third, you are now in charge of memory management of that std::vector. It could be a reference to a std::vector that is "owned" somewhere else, or something that is "owned" locally. (You should not place vector pointers of both types in the same structure).

//After this, i also want coordinate (2,2,2) to to store value , it CREATE and POINT to another new std::vector

Ok? So you create a std::vector using new, then store it at (2,2,2) if it doesn't already exist...

Note that if you changed your array to:
std::vector<float> cubes[16][16][16];

you no longer have to worry about memory management nearly as much. And unless you are creating very sparse or memory-starved programs, the cost won't be that high.

It will mean you can't use the "non-own" semantics, but ...

If you follow that approach, then you know if [j][k] is empty if
if ( cubes[j][k].empty() ) {  cout << "Empty!\n";};

ie: your code ends up being a bit simpler.

[Edited by - NotAYakk on March 24, 2008 6:39:04 PM]

Share on other sites
Doesn't boost have a multidimensional array container?

Would probably be a lot more suited to what you're trying to do

Share on other sites
Quote:
 Original post by Stab-o-tronDoesn't boost have a multidimensional array container?

boost::multi_array

1. 1
Rutin
24
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 19
• 11
• 11
• 9
• Forum Statistics

• Total Topics
631760
• Total Posts
3002176
×

Important Information

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!