CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2008
    Posts
    44

    Dynamic Multidimensional Array

    Hi,

    How do I create a multidimensional array[r][c] with a dynamic size? My input file contains the r and c values, but can be different for each input file.

    I know that you can specify a dynamic array within a function (such as my readfile function, but how do I pass that array to another function?

    Thanks

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Dynamic Multidimensional Array

    There are several ways to do it; the easiest is to just use a vector< vector< int > > as your container. This is recommended if you want to be able to resize both dimensions independently.

    If you will only ever resize both dimensions at once, there's a slightly more efficient way to do it using two variables, a vector<int> and a vector<int*>. I can elaborate on that if you like.

    Of course, it's also possible to do this using a plain old array.

  3. #3
    Join Date
    Jun 2008
    Posts
    44

    Re: Dynamic Multidimensional Array

    I m not familiar with the vector container at all. I found another way to do it - to basically pass the array from function to function.

    Do you think that would cause significant processing time? My data will become large, prob 800 x 800 arrays.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Dynamic Multidimensional Array

    Quote Originally Posted by mbanghart
    Hi,

    How do I create a multidimensional array[r][c] with a dynamic size?
    Did you do a search on CodeGuru? There are at least 50 to 100 threads that show how to do this, with complete examples.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jun 2008
    Posts
    44

    Re: Dynamic Multidimensional Array

    Actually think vector might be what I need.

    How do I populate the array since its two dimensional?

    I will look around on codeguru.

  6. #6
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    Re: Dynamic Multidimensional Array

    Quote Originally Posted by Paul McKenzie
    Did you do a search on CodeGuru? There are at least 50 to 100 threads that show how to do this, with complete examples.

    Regards,

    Paul McKenzie
    To be fair, the search feature of this forum (and most forums) is useless, which is why in the past I've shyed away from assuming that someone did not try to search the forums before posting. Fortunately, a google search containing the keyword "codeguru" or "site:www.codeguru.com" can be very effective, so I'd recommend that instead.
    - Alon

  7. #7
    Join Date
    Jun 2008
    Posts
    44

    Re: Dynamic Multidimensional Array

    I managed to get it working. How can I delete an entire row from a multidimensional vector:

    Here is how I initialize inputMatrix:

    Code:
        vector<int> v(columns);
         vector<vector <int> > inputMatrix(rows,v);
    Other assumptions: Rows and columns is set somewhere else. inputMatrix is populated correctly when the following code executes:

    Here is my code:
    Code:
     //Start from top
         for (r = 0; r < rows; r++)
         {
           for (c = 0; c < columns; c++)
           {
               if (inputMatrix[r][c] == 1)
                   oneCounter++;
           }//inner for c
           
           if (oneCounter < pruneThreshold)
              {
                  if (stopFlag == true)
                     break;
                  
                  else 
                  {                                                     
                                                                        
                  //delete entire row r
                  //UNSURE
                  
                  }//else
              }//if
              else 
              {
                   //set flag to ensure data integrity
                   stopFlag = true;
              }//else
                   
         }//outer for

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Dynamic Multidimensional Array

    Quote Originally Posted by mbanghart
    I found another way to do it - to basically pass the array from function to function.

    Do you think that would cause significant processing time? My data will become large, prob 800 x 800 arrays.
    There's a subtlety here you need to be aware of. When you're passing around a plain old array, you aren't copying the *array* from function to function, just the pointer to it. Therefore, it's very cheap to pass it to a function.

    But when you're using a std::vector, then the entire array *will* be copied if you let it-----and that's very expensive, so it's best to avoid it. For this reason, std::vectors (and all STL containers, actually) should always be passed by reference, not by value. Pass by const reference if you don't want to change it, but still by reference. The only time you should pass by value is if you would otherwise make a duplicate of the container inside the function anyway.

    How can I delete an entire row from a multidimensional vector:
    All you have to do is delete one of the vectors inside the outer vector. This could be done by:

    Code:
    inputMatrix.erase(inputMatrix.begin()+r);
    However, this is not something you should do often, because (as always with array-like things), it requires all subsequent elements to be copied backwards one position. Since you're talking about copying entire vectors, that can be costly.

    There is an optimization you could do if you need speed; maybe the STL implementation would even do it itself, although I doubt it. That would be to essentially "bubble" the offending row to the bottom of the matrix using the vector::swap function, and then just call inputMatrix.pop_back(). However, as always, don't try such optimizations until you've determined that the "clean" way of writing the code is too slow.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured