How do you dynamically allocate a matrix?

前端 未结 11 864
轻奢々
轻奢々 2020-11-29 01:29

How do you dynamically allocate a 2D matrix in C++? I have tried based on what I already know:

#include 

int main(){
    int rows;
    int c         


        
相关标签:
11条回答
  • 2020-11-29 01:37
    arr = new int[cols*rows];
    

    If you either don't mind syntax

    arr[row * cols + col] = Aij;
    

    or use operator[] overaloading somewhere. This may be more cache-friendly than array of arrays, or may be not, more probably you shouldn't care about it. I just want to point out that a) array of arrays is not only solution, b) some operations are more easier to implement if matrix located in one block of memory. E.g.

    for(int i=0;i < rows*cols;++i)
       matrix[i]=someOtherMatrix[i];
    

    one line shorter than

    for(int r=0;i < rows;++r)
      for(int c=0;i < cols;++s)
         matrix[r][c]=someOtherMatrix[r][c];
    

    though adding rows to such matrix is more painful

    0 讨论(0)
  • 2020-11-29 01:40
    const int nRows = 20;
    const int nCols = 10;
    int (*name)[nCols] = new int[nRows][nCols];
    std::memset(name, 0, sizeof(int) * nRows * nCols); //row major contiguous memory
    name[0][0] = 1; //first element
    name[nRows-1][nCols-1] = 1; //last element
    delete[] name;
    
    0 讨论(0)
  • 2020-11-29 01:44

    Try boost::multi_array

    #include <boost/multi_array.hpp>
    
    int main(){
        int rows;
        int cols;
        boost::multi_array<int, 2> arr(boost::extents[rows][cols] ;
    }
    
    0 讨论(0)
  • 2020-11-29 01:44

    I have this grid class that can be used as a simple matrix if you don't need any mathematical operators.

    /**
     * Represents a grid of values.
     * Indices are zero-based.
     */
    template<class T>
    class GenericGrid
    {
        public:
            GenericGrid(size_t numRows, size_t numColumns);
    
            GenericGrid(size_t numRows, size_t numColumns, const T & inInitialValue);
    
            const T & get(size_t row, size_t col) const;
    
            T & get(size_t row, size_t col);
    
            void set(size_t row, size_t col, const T & inT);
    
            size_t numRows() const;
    
            size_t numColumns() const;
    
        private:
            size_t mNumRows;
            size_t mNumColumns;
            std::vector<T> mData;
    };
    
    
    template<class T>
    GenericGrid<T>::GenericGrid(size_t numRows, size_t numColumns):
        mNumRows(numRows),
        mNumColumns(numColumns)
    {
        mData.resize(numRows*numColumns);
    }
    
    
    template<class T>
    GenericGrid<T>::GenericGrid(size_t numRows, size_t numColumns, const T & inInitialValue):
        mNumRows(numRows),
        mNumColumns(numColumns)
    {
        mData.resize(numRows*numColumns, inInitialValue);
    }
    
    
    template<class T>
    const T & GenericGrid<T>::get(size_t rowIdx, size_t colIdx) const
    {
        return mData[rowIdx*mNumColumns + colIdx];
    }
    
    
    template<class T>
    T & GenericGrid<T>::get(size_t rowIdx, size_t colIdx)
    {
        return mData[rowIdx*mNumColumns + colIdx];
    }
    
    
    template<class T>
    void GenericGrid<T>::set(size_t rowIdx, size_t colIdx, const T & inT)
    {
        mData[rowIdx*mNumColumns + colIdx] = inT;
    }
    
    
    template<class T>
    size_t GenericGrid<T>::numRows() const
    {
        return mNumRows;
    }
    
    
    template<class T>
    size_t GenericGrid<T>::numColumns() const
    {
        return mNumColumns;
    }
    
    0 讨论(0)
  • 2020-11-29 01:47
     #include <iostream>
    
        int main(){
            int rows=4;
            int cols=4;
            int **arr;
    
            arr = new int*[rows];
            for(int i=0;i<rows;i++){
               arr[i]=new int[cols];
            }
            // statements
    
            for(int i=0;i<rows;i++){
               delete []arr[i];
            }
            delete []arr;
            return 0;
         }
    
    0 讨论(0)
  • 2020-11-29 01:49

    The other answer describing arrays of arrays are correct.
    BUT if you are planning of doing a anything mathematical with the arrays - or need something special like sparse matrices you should look at one of the many maths libs like TNT before re-inventing too many wheels

    0 讨论(0)
提交回复
热议问题