如何使用 new 在 C++ 中宣告一個二維陣列
動態二維陣列基本上是陣列指標的陣列。這是維度為 3 x 4 的二維陣列的圖表。

演算法
Begin Declare dimension of the array. Dynamic allocate 2D array a[][] using new. Fill the array with the elements. Print the array. Clear the memory by deleting it. End
示例程式碼
#include <iostream>
using namespace std;
int main() {
int B = 4;
int A = 5;
int** a = new int*[B];
for(int i = 0; i < B; ++i)
a[i] = new int[A];
for(int i = 0; i < B; ++i)
for(int j = 0; j < A; ++j)
a[i][j] = i;
for(int i = 0; i < B; ++i)
for(int j = 0; j < A; ++j)
cout << a[i][j] << "\n";
for(int i = 0; i < A; ++i)
delete [] a[i];
delete [] a;
return 0;
}輸出
0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP