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;
}