What is the method for creating a two-dimensional array…

In C++, there are several methods to create a two-dimensional array:

  1. You can create a two-dimensional array by specifying the number of rows and columns using array declaration syntax. For example:
int array[3][4]; // 创建一个3行4列的二维整数数组
  1. fresh
int **array;
int rows = 3;
int cols = 4;
array = new int*[rows]; // 先分配行数
for (int i = 0; i < rows; i++) {
    array[i] = new int[cols]; // 再分配列数
}
  1. a quantity that has both magnitude and direction
#include <vector>
...
int rows = 3;
int cols = 4;
std::vector<std::vector<int>> array(rows, std::vector<int>(cols));

In these methods, using array declaration syntax and dynamic memory allocation both require determining the number of rows and columns beforehand, while using vector containers allows for dynamically adjusting the size of the array.

bannerAds