C++动态二维数组演示的代码

356 阅读1分钟
将代码过程中经常用到的代码珍藏起来,下边资料是关于C++动态二维数组演示的代码。 

#include <iostream>
#include <string>

using namespace std;

{
	for( int i = 0; i < x; i++ )
	{
		List[i] = new int[y];
		for( int j = 0; j < y; j++ )
		{
			List[i][j] = 0;
		}
	}
	for( int i = 0; i < xmax; i++ )
	{
		for( int j = 0; j <= ycur; j++ )	
		{
			List[i][j] = TEMP[i][j]; 
		}
	}
	for( int i = 0; i < ycur; i++ )
	{
		delete[] TEMP[i];
		TEMP[i] = 0;
	}
	delete[] TEMP;
	TEMP = 0;
}

int main()
{
	string input;
	int xsize = 1, ysize = 1;
	int xcur = 0, ycur = 0, xmax = 0;
	cout << "Enter some integers (input 'r' for new row and 's' for stop):" << endl;
	while(cin >> input)
	{
			break;
		{
			ysize++;
			xcur = 0;
			ycur += 1;
		}
		else
		{
			xsize++;
			xcur++;
				xmax = xcur;
		}
	}
	for( int j = 0; j <= ycur; j++ )
	{
		for( int i = 0; i < xmax; i++ )
		{
			cout << List[i][j] << " ";
		}
		cout << endl;
	}
	system("PAUSE");
	return 0;
}