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 <iostream>
#include <vector>
#include <iomanip>
using namespace std;
int main()
{
int i,
j,
m, //行数
n; //列数
cout << "input value for m,n:";
cin>>m>>n;
//注意下面这一行:vector<int后两个">"之间要有空格!否则会被认为是重载">>"。
vector<vector<int> > vecInt(m, vector<int>(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<<setw(5)<<vecInt[i][j]<<":"<<setw(9)<<&vecInt[i][j];
cout<<endl;
}
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 <iostream>
#include <vector>
#include <iomanip>
using namespace std;
int main()
{
int i,
j,
k,
m, //一维坐标
n, //二维坐标
l; //三维坐标
cout << "input value for m,n,l:";
cin>>m>>n>>l;
vector<vector<vector<int> > > vecInt(m, vector<vector<int> >(n, vector<int>(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<l; k++)
cout<<setw(5)<<vecInt[i][j][k]<<":"<<setw(9)<<&vecInt[i][j][k];
cout<<endl;
}
cout<<endl;
}