首先介绍一下双指针方法,在这里双指针就是指像指针的指针,比如你可以这样声明一个数组: int **p = new int*[num1]; 而对每一个*p(一共num1个*p)申请一组内存空间: for(int i=0; i p[i] = new int[num2]; 其中,num1是行数,num2是数组的列数。测试的源程序如下:
//文件名: array04.cpp #include #include using namespace std;
int main() { int num1,//行数 num2;//列数
cout<<"Please enter the number for row and column: "< cin >> num1 >> num2;
//为二维数组开辟空间 int **p = new int*[num1]; for(int i=0; i p[i] = new int[num2];
Please enter the number for row and column: 4 5 1:004915F0 2:004915F4 3:004915F8 4:004915FC 5:00491600 2:00491180 4:00491184 6:00491188 8:0049118C 10:00491190 3:00491140 6:00491144 9:00491148 12:0049114C 15:00491150 4:00491100 8:00491104 12:00491108 16:0049110C 20:00491110 Press any key to continue
//文件名: array05.cpp #include #include #include using namespace std; int main() { int i, j, m, //行数 n; //列数
cout << "input value for m,n:"; cin>>m>>n; //注意下面这一行:vector"之间要有空格!否则会被认为是重载">>"。 vector > vecInt(m, vector(n)); for (i = 0; i < m; i++) for (j = 0; j < n; j++) vecInt[i][j] = i*j; for (i = 0; i < m; i++) { for (j = 0; j < n; j++) cout< cout< }
return 0; }
以下是运行结果:
input value for m,n:3 4 0: 00491180 0: 00491184 0: 00491188 0: 0049118C 0: 00491140 1: 00491144 2: 00491148 3: 0049114C 0: 00491100 2: 00491104 4: 00491108 6: 0049110C Press any key to continue
//文件名: array06.cpp #include #include #include using namespace std; int main() { int i, j, k, m, //一维坐标 n, //二维坐标 l; //三维坐标
cout << "input value for m,n,l:"; cin>>m>>n>>l; vector > > vecInt(m, vector >(n, vector(l))); for (i = 0; i < m; i++) for (j = 0; j < n; j++) for(k = 0; k < l; k++) vecInt[i][j][k] = i+j+k; for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { for(k = 0; k cout< cout< } cout< }