How do you dynamically allocate a matrix?

前端 未结 11 979
轻奢々
轻奢々 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:55

    or you can just allocate a 1D array but reference elements in a 2D fashion:

    to address row 2, column 3 (top left corner is row 0, column 0):

    arr[2 * MATRIX_WIDTH + 3]

    where MATRIX_WIDTH is the number of elements in a row.

提交回复
热议问题