942,959 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 5181
  • C++ RSS
Jul 31st, 2008
0

iterate a 3d vector

Expand Post »
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]

C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Posting Whiz in Training
Lukezzz is offline Offline
268 posts
since Mar 2008
Jul 31st, 2008
0

Re: iterate a 3d vector

This works for me
C++ Syntax (Toggle Plain Text)
  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. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5591
Solved Threads: 2280
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,930 posts
since Aug 2005
Jul 31st, 2008
0

Re: iterate a 3d vector

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.
Reputation Points: 10
Solved Threads: 1
Posting Whiz in Training
Lukezzz is offline Offline
268 posts
since Mar 2008
Jul 31st, 2008
0

Re: iterate a 3d vector

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 !

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 1
Posting Whiz in Training
Lukezzz is offline Offline
268 posts
since Mar 2008
Jul 31st, 2008
0

Re: iterate a 3d vector

If you want a real 3D array C++ surrogate then try indicies, not iterators:
cpp Syntax (Toggle Plain Text)
  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. }
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jul 31st, 2008
0

Re: iterate a 3d vector

Apropos, dont tread on typedefs: they never create new types, they are type aliases only...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: c++ queries
Next Thread in C++ Forum Timeline: Passing infile values??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC