how to create a 3d address array which pointed to a single std::vector?
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.
Quote:Original post by ToohrVyk
std::vector<T> *cubes[4][4][4] = {0};
T is??
and how can i create a new vector by this?
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.
Quote:Original post by ToohrVykQuote: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]
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
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
First, when making an array of pointers, you might want to zero-initialize:
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).
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:
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
ie: your code ends up being a bit simpler.
[Edited by - NotAYakk on March 24, 2008 6:39:04 PM]
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]
Doesn't boost have a multidimensional array container?
Would probably be a lot more suited to what you're trying to do
Would probably be a lot more suited to what you're trying to do
Quote:Original post by Stab-o-tron
Doesn't boost have a multidimensional array container?
boost::multi_array
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement