-
iterate a 3d vector
I have declared a vector with 3 Dimensions with the first code below.
Then I have managed to push_back all the 3 dimensions so my vector is filled up like this:
vector1[2][2][2]
Code:
typedef std::vector<string> String1D;
typedef std::vector<String1D> String2D;
typedef std::vector<String2D> String3D;
String3D vector1;
What I now need to do is to iterate from vector1[0][0][0] until vector1[2][2][2]
with the code below.
But I get a huge of compileerrors. I think The code below can work if you havn´t declared the vector with typedef but I am not sure really.
I wonder if I am near a solution for this ? It is difficult to find examples for 3D vectors.
Code:
std::vector< std::vector<string> >::iterator OneDStart = vector1.begin();
std::vector< std::vector<string> >::iterator OneDEnd = vector1.end();
for ( ; OneDStart != OneDEnd; OneDStart++ )
{ //1D
std::vector<string>::iterator TwoDStart = OneDStart->begin();
std::vector<string>::iterator TwoDEnd = OneDStart->end();
for ( ; TwoDStart != TwoDEnd; TwoDStart++ )
{ //2D
std::vector<string>::iterator ThreeDStart = TwoDStart->begin();
std::vector<string>::iterator ThreeDEnd = TwoDEnd->end();
for ( ; ThreeDStart != ThreeDEnd; ThreeDStart++ )
{ //3D
} //1D
} //2D
} //3D
-
I would expect something along these lines:
Code:
for (String3D::iterator i = vector1.begin(); i != vector1.end(); ++i)
{
for (String2D::iterator j = i->begin(); j != i->end(); ++j)
{
for (String1D::iterator k = j->begin(); k != j->end(); ++k)
{
std::cout << *k << std::endl;
}
}
}
-
Your iterator types are wrong. The type of the iterator must match with the container you are going to iterate.
Why don't you use your typedefs to declare the correct iterators?
-
Now I understand. The code works great. I have to use that containers to iterate through them.
Thanks LaserLight and anon !
-
Just curious, why are you using push_back is you know your vector will be 3x3x3? Can't you just use the constructor?
Code:
String3D vector1(3, String2D(3, String1D(3)));
-
Maybe iterate over the number of forums the message has been posted to?
http://www.daniweb.com/forums/thread137814.html
-
Ok :) The thing is that I have a program that create files with text inside. Each files text will be put in 3:rd dimension of the vector.
Actually all text will be put in the 3:rd dimension and 1:st and 2:nd will just be used as a counter.
The thing is that I will never know how many files that the application will create. But all these files will be red into the vector with the pushback method.
Then I will iterate the 3:rd dimension from begin to end for the first and second dimension.
If some things will be true and false in the 3:rd dimension with 2:nd dimensions count in considiration. I will break; the loop to save time.
So the push_back method I use to save time to not iterate through emty elements.
It will really be a timesaving technique for the purpose.
I read long textfiles and this will save a lot of time I beleive.
-
(Sometimes many different angles of the same problem is good especially when I find it difficult to get a good solution to as an important function. Nothing bad with that I hope. This type of iteration will save months of calculations.).
-
-
Salem, I have red what you was sending me and I do understand. It might be wrong of me to do.
I did not meen anything bad about it to know anyway. I learn from the info : )
Thank you for this information and ofcourse for the great help on this question.
Nice Day!
-
>> The thing is that I will never know how many files that the application will create.
Ok, makes sense.
-
There has to be a container in the STL more suited for this task. Perhaps a map of vectors would simplify this?
-
boost has an option called MultiArray that is probably better.
http://www.boost.org/doc/libs/1_35_0...doc/index.html