Sign in to follow this  
gohkgohk

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

Recommended Posts

gohkgohk    100
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 this post


Link to post
Share on other sites
ToohrVyk    1595
Quote:
Original post by gohkgohk
T 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 this post


Link to post
Share on other sites
gohkgohk    100
Quote:
Original post by ToohrVyk
Quote:
Original post by gohkgohk
T 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 this post


Link to post
Share on other sites
gohkgohk    100
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 this post


Link to post
Share on other sites
NotAYakk    876
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).

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

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 [i][j][k] is empty if

if ( cubes[i][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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this