C /C++の多次元配列
C / C ++では、多次元配列は簡単な言葉で配列の配列として定義されます。多次元配列では、データは表形式で(行の主要な順序で)格納されます。次の図は、次元が3 x 3x3の多次元配列のメモリ割り当て戦略を示しています。
アルゴリズム
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
-
C /C++での多次元配列の初期化
多次元配列では、配列の次元は1より大きい必要があります。次の図は、次元が3 x 3x3の多次元配列のメモリ割り当て戦略を示しています。 これは、多次元配列を初期化するためのC++プログラムです。 アルゴリズム Begin Initialize the elements of a multidimensional array. Print the size of the array. Display the content of the array. End 例 #include<iostream>
-
Cの多次元配列
ここに多次元配列が表示されます。配列は基本的に同種のデータのセットです。それらは連続したメモリ位置に配置されます。さまざまなケースで、配列が1次元ではないことがわかります。 2次元または多次元の形式で配列を作成する必要がある場合があります。 多次元配列は、2つの異なるアプローチで表すことができます。これらは行メジャーアプローチであり、もう1つは列メジャーアプローチです。 r行c列の2次元配列を考えてみましょう。配列内の要素の数はn=r*cです。 0≤i