iterate a 3d vector

This is a discussion on iterate a 3d vector within the C++ Programming forums, part of the General Programming Boards category; I have declared a vector with 3 Dimensions with the first code below. Then I have managed to push_back all ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    383

    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&#180;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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    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;
            }
        }
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Now I understand. The code works great. I have to use that containers to iterate through them.
    Thanks LaserLight and anon !

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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)));

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,488
    Maybe iterate over the number of forums the message has been posted to?
    http://www.daniweb.com/forums/thread137814.html
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    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.
    Last edited by Coding; 07-31-2008 at 10:19 AM.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    (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.).

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,488
    At the risk of not being considerate
    http://www.catb.org/~esr/faqs/smart-...ons.html#forum

    Or the risk of appearing to claim urgency
    http://www.catb.org/~esr/faqs/smart-...ns.html#urgent
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    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!

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> The thing is that I will never know how many files that the application will create.
    Ok, makes sense.

  12. #12
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    There has to be a container in the STL more suited for this task. Perhaps a map of vectors would simplify this?

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    boost has an option called MultiArray that is probably better.

    http://www.boost.org/doc/libs/1_35_0...doc/index.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  4. My 3D Vector class
    By Lurker in forum C++ Programming
    Replies: 105
    Last Post: 11-16-2003, 04:58 PM
  5. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 06:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21