Page 1 of 3 123 LastLast
Results 1 to 10 of 23

Thread: [SOLVED] Dynamic Multidimension array?

  1. #1
    Join Date
    Feb 2007
    Beans
    281

    Question [SOLVED] Dynamic Multidimension array?

    C++: Recently, I have come to need (or at least think I do) to put a resizable multidimensional array into a class. Now I know that using vector arrays isn't a good way to do this... what would be a good way to do this?

  2. #2
    Join Date
    Jul 2005
    Beans
    1,535
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Dynamic Multidimension array?

    Quote Originally Posted by Zeotronic View Post
    C++: Recently, I have come to need (or at least think I do) to put a resizable multidimensional array into a class.
    First step is to know what you need.

    Now I know that using vector arrays isn't a good way to do this... what would be a good way to do this?
    I would always suggest using STL vectors instead of C style arrays, so I don't know what you mean here. For an example:

    PHP Code:
    template <typename T>
    class 
    Matrix{
      private:
         
    std::vector<std::vector<T> > multiArray;  
    }; 
    When I invented the Web, I didn't have to ask anyone's permission.
    ~Tim Berners-Lee on Net Neutrality
    -------------------------------------
    Visit the Ubuntu Programming IRC-channel at #ubuntu-programming (chat.freenode.net).

  3. #3
    Join Date
    Feb 2007
    Beans
    281

    Unhappy Re: Dynamic Multidimension array?

    I would always suggest using STL vectors instead of C style arrays, so I don't know what you mean here. For an example:
    I've searched the subject rather thoroughly trying to solve my problem myself, and I'm pretty sure your example turns out like crap. Each array contained in an array (in your example) would have to be resized to the proper size to have a standard sized second dimension, would they not? And I need to work with up to 3 or 4 dimensions, so this approach simply would not do.

    But that is... as I said... to my knowledge.

  4. #4
    Join Date
    Jan 2009
    Beans
    11

    Thumbs up Re: Dynamic Multidimension array?

    Quote Originally Posted by Zeotronic View Post
    C++: Recently, I have come to need (or at least think I do) to put a resizable multidimensional array into a class. Now I know that using vector arrays isn't a good way to do this... what would be a good way to do this?
    Perhaps something like this:
    Code:
    //create 5x5 array
    int** array = new int*[5];
    int row1[5] = {1,2,3,4,5};
    int row2[5] = {6,7,8,9,10};
    ...
    array[0]= &row1[0];
    array[1] = &row2[0];
    
    cout << array[0][1]; //2
    cout << array[1][2]; //8
    
    //change to 6x6 array;
    int** temp = array;
    delete[] array;
    array = new int*[6];
    ...//copy data
    Does this help? With this model, its easy to make a 5 dimensional array where each branch is a different length. You can also generalizes this to more dimensions with int*** array or int**** array:

    Code:
    int*** array = new int**[5];
    int** branch1 = new int*[5];
    int branch1row1[5] = {1,2,3,4,5};
    branch1[0] = &branch1row1[0];
    array[0] = &branch1[0];
    cout << array[0][0][2]; //3
    Last edited by hundredwatt; January 7th, 2009 at 11:26 PM. Reason: Debugging...

  5. #5
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,715

    Re: Dynamic Multidimension array?

    How about a hashtable that takes some two-value X,Y object as keys? In this case, you could expand it indefinitely, and you can just assume some default value for all the keys that aren't set.

  6. #6
    Join Date
    Mar 2007
    Beans
    Hidden!
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Dynamic Multidimension array?

    What are you doing that you think you need 3-4 dimensions for? If you let us know what it is that you're aiming for we can likely be of more help to you.

    And the thing with vectors is that they resize themselves, you don't have to manage it. If you need another item, push it on.

  7. #7
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Dynamic Multidimension array?

    Quote Originally Posted by Zeotronic View Post
    I've searched the subject rather thoroughly trying to solve my problem myself, and I'm pretty sure your example turns out like crap.
    Ouch; tough remark to make to someone considering that person was trying to help you.

  8. #8
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Dynamic Multidimension array?

    The easiest solution when dealing with multidimensional arrays is to use the array type as defined in the boost libraries.

    Here's an example:
    PHP Code:
    #include <boost/array.hpp>
    #include <iostream>

    int main()
    {
      
    typedef boost::array<int,    3OneD;
      
    typedef boost::array<OneD,   3TwoD;
      
    typedef boost::array<TwoD,   3ThreeD;
      
    typedef boost::array<ThreeD3FourD;

      
    FourD array4D;

      
    array4D[0][0][0][0] = 10;
      
    array4D[1][1][1][1] = 20;
      
    array4D[2][2][2][2] = 30;

      
    std::cout << "array4D[0][0][0][0] = " << array4D[0][0][0][0] << std::endl;
      
    std::cout << "array4D[1][1][1][1] = " << array4D[1][1][1][1] << std::endl;
      
    std::cout << "array4D[2][2][2][2] = " << array4D[2][2][2][2] << std::endl;

      return 
    0;


  9. #9
    Join Date
    Feb 2007
    Beans
    281

    Re: Dynamic Multidimension array?

    Ouch; tough remark to make to someone considering that person was trying to help you.
    I'm sorry I'm so blunt.

    But am I not correct in that, using so may vector arrays just to get a 3 dimensional array would incur vast amounts of waste? We're talking all the extra data required by a vector, multiplied by what?
    What are you doing that you think you need 3-4 dimensions for?
    As far as the 3 dimensions goes. I'm storing voxel information... I think I've taken the instance where I needed 4 dimensions out of my code.
    How about a hashtable that takes some two-value X,Y object as keys?
    I'm not familiar with hashtables.
    Perhaps something like this:
    I'll toy around with that, but wouldn't "int** temp = array;" merely point to what's contained by array, and then, of course, it would be deleted in the next line?
    The easiest solution when dealing with multidimensional arrays is to use the array type as defined in the boost libraries.
    Array handles multidimensional arrays? I thought only Multi_Array did!.. and I couldn't get that to work in a structure... this will work?

    ...Oh wait... I see. Wouldn't that be almost as bad as vector?
    Last edited by Zeotronic; January 8th, 2009 at 02:07 AM.

  10. #10
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,715

    Re: Dynamic Multidimension array?

    Quote Originally Posted by Zeotronic View Post
    I'm not familiar with hashtables.
    You can use std::map as the hashtable, and std:air for the keys of a 2-dimensional table.

    Code:
    #include <map>
    #include <iostream>
    
    typedef std::pair<int,int> vec2;
    
    int main() {
    	std::map<vec2, int> grid;
    	grid[vec2(10, 10)] = 10;
    	grid[vec2(10000, 10000)] = 7;
    	std::cout << grid[vec2(10, 10)] << std::endl;
    	std::cout << grid[vec2(10000, 10000)] << std::endl;
    	return 0;
    }
    For larger dimensions you can just make your own key class or use something like boosts tuple.

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •