iterate a 3d vector

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2008
Posts: 167
Reputation: Lukezzz is an unknown quantity at this point 
Solved Threads: 1
Lukezzz Lukezzz is offline Offline
Junior Poster

iterate a 3d vector

 
0
  #1
Jul 31st, 2008
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]

  1. typedef std::vector<string> String1D;
  2. typedef std::vector<String1D> String2D;
  3. typedef std::vector<String2D> String3D;
  4.  
  5. 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.
  1. std::vector< std::vector<string> >::iterator OneDStart = vector1.begin();
  2. std::vector< std::vector<string> >::iterator OneDEnd = vector1.end();
  3.  
  4. for ( ; OneDStart != OneDEnd; OneDStart++ )
  5. { //1D
  6. std::vector<string>::iterator TwoDStart = OneDStart->begin();
  7. std::vector<string>::iterator TwoDEnd = OneDStart->end();
  8.  
  9. for ( ; TwoDStart != TwoDEnd; TwoDStart++ )
  10. { //2D
  11. std::vector<string>::iterator ThreeDStart = TwoDStart->begin();
  12. std::vector<string>::iterator ThreeDEnd = TwoDEnd->end();
  13.  
  14. for ( ; ThreeDStart != ThreeDEnd; ThreeDStart++ )
  15. { //3D
  16.  
  17.  
  18. } //1D
  19. } //2D
  20. } //3D
Last edited by Lukezzz; Jul 31st, 2008 at 10:56 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,330
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1453
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: iterate a 3d vector

 
0
  #2
Jul 31st, 2008
This works for me
  1. #include <vector>
  2. #include <string>
  3. using namespace std;
  4.  
  5.  
  6. typedef std::vector<string> String1D;
  7. typedef std::vector<String1D> String2D;
  8. typedef std::vector<String2D> String3D;
  9.  
  10. int main()
  11. {
  12. String3D vector1;
  13. String3D::iterator OneDStart = vector1.begin();
  14. String3D::iterator OneDEnd = vector1.end();
  15.  
  16. for ( ; OneDStart != OneDEnd; OneDStart++ )
  17. { //1D
  18. String2D::iterator TwoDStart = OneDStart->begin();
  19. String2D::iterator TwoDEnd = OneDStart->end();
  20. for ( ; TwoDStart != TwoDEnd; TwoDStart++ )
  21. { //2D
  22. String1D::iterator ThreeDStart = TwoDStart->begin();
  23. String1D::iterator ThreeDEnd = TwoDEnd->end();
  24. for ( ; ThreeDStart != ThreeDEnd; ThreeDStart++ )
  25. { //3D
  26.  
  27.  
  28. } //1D
  29. } //2D
  30. } //3D
  31. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 167
Reputation: Lukezzz is an unknown quantity at this point 
Solved Threads: 1
Lukezzz Lukezzz is offline Offline
Junior Poster

Re: iterate a 3d vector

 
0
  #3
Jul 31st, 2008
That was fantastico, I saw the adjustments you did to the code and it compiles fine.
When I run the code I am getting an Error message that says:

"Expression: vector iterator not dereferencable"

I dont know what could be ment by this ?
Last edited by Lukezzz; Jul 31st, 2008 at 12:03 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 167
Reputation: Lukezzz is an unknown quantity at this point 
Solved Threads: 1
Lukezzz Lukezzz is offline Offline
Junior Poster

Re: iterate a 3d vector

 
0
  #4
Jul 31st, 2008
I tried to do this and this works but I dont understand the logic. For me it should be 1D first and 3D last but I begin to iterate the 3:rd dimension at top of code from what I understand.
Shouldn´t I iterate the 1:st dimension first or does this happening but I cant see it ?
It iterates from vector1[0][0][0] until vector[2][2][2]. It is correct !

  1. for (String3D::iterator i = vector1.begin(); i != vector1.end(); ++i)
  2. {
  3. for (String2D::iterator j = i->begin(); j != i->end(); ++j)
  4. {
  5. for (String1D::iterator k = j->begin(); k != j->end(); ++k)
  6. {
  7. std::string GetText = *k;
  8. }
  9. }
  10. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: iterate a 3d vector

 
0
  #5
Jul 31st, 2008
If you want a real 3D array C++ surrogate then try indicies, not iterators:
  1. // int base for simplicity
  2. typedef std::vector<int> int1D;
  3. typedef std::vector<int1D> int2D;
  4. typedef std::vector<int2D> int3D;
  5.  
  6. int main()
  7. {
  8. int dim = 3;
  9. int3D v;
  10. // initiate the cube so v[i][j][k] == ijk...
  11. v.resize(dim);
  12. for (int i = 0; i < (int)v.size(); ++i)
  13. {
  14. v[i].resize(dim);
  15. for (int j = 0; j < (int)v[i].size(); ++j)
  16. {
  17. v[i][j].resize(dim);
  18. for (int k = 0; k < (int)v[i][j].size(); ++k)
  19. v[i][j][k] = (i+1)*100+(j+1)*10+(k+1);
  20. }
  21. }
  22.  
  23. // print plane by plane then modify cube elements
  24. for (int i = 0; i < (int)v.size(); ++i)
  25. {
  26. cout << i << ":\n"; // i-th plane
  27. for (int j = 0; j < (int)v.size(); ++j)
  28. {
  29. for (int k = 0; k < (int)v.size(); ++k)
  30. {
  31. cout << '\t' << v[i][j][k];
  32. v[i][j][k] = 1; // why? See below
  33. }
  34. cout << "\n";
  35. }
  36. }
  37. cout << endl;
  38.  
  39. // calculate sum of elements i*j*k*1 then go to Fortran...
  40. int sum = 0;
  41. for (size_t i = 0; i < v.size(); ++i)
  42. for (size_t j = 0; j < v[i].size(); ++j)
  43. for (size_t k = 0; k < v[i][j].size(); ++k)
  44. sum += v[i][j][k];
  45. // must be 3*3*3 == 27...
  46. cout << "Sum(v): " << sum << endl;
  47.  
  48. return 0;
  49. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: iterate a 3d vector

 
0
  #6
Jul 31st, 2008
Apropos, dont tread on typedefs: they never create new types, they are type aliases only...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC