bate's blog

調べたこと実装したことなどを取りとめもなく書きます。

C++で2次元配列を動的に

下記のようになるが、納得し切れていない。

#include <iostream>

using namespace std;

int main()
{
	// 配列の行:rowと列:column
	int row=0, column=0;
	cout << "行の数を入力 >";
	cin >> row;
	cout << "列の数を入力 >";
	cin >> column;

	// 生成
	int **array = new int*[row];
	for( int i = 0; i < row; ++i )
	{
		array[i] = new int[column];
	}

	// 代入
	int count = 0;
	for( int i = 0; i < row; ++i )
		for( int j = 0; j < column; ++j )
		{
			array[i][j] = count;
			++count;
		}

	// 表示
	for( int i = 0; i < row; ++i )
	{
		for( int j = 0; j < column; ++j )
			cout << array[i][j] << " ";
		cout << endl;
	}

	// 解放
	for( int i = 0; i < row; ++i )
		delete [] array[i];
	delete [] array;

	return 0;
}