# c++使用vector或指针创建多维数组

## STL vector

#include <vector>
using namespace std;

int main() {
// construct array2D[HEIGHT][WIDTH]
vector<vector<double> > array2D;

array2D.resize(HEIGHT);
for (int i = 0; i < HEIGHT; ++i)
array2D[i].resize(WIDTH);

// construct array3D[HEIGHT][WIDTH][DEPTH]
vector<vector<vector<double> > > array3D;

array3D.resize(HEIGHT);
for (int i = 0; i < HEIGHT; ++i) {
array3D[i].resize(WIDTH);
for (int j = 0; j < WIDTH; ++j)
array3D[i][j].resize(DEPTH);
}

return 0;
}

## 指针

int main() {
// construct p2DArray[HEIGHT][WIDTH]
double **p2DArray;

p2DArray = new double*[HEIGHT];
for (int i = 0; i < HEIGHT; ++i)
p2DArray[i] = new double[WIDTH];

for (int i = 0; i < HEIGHT; ++i)
delete [] p2DArray[i];
delete [] p2DArray;

// construct p3DArray[HEIGHT][WIDTH][DEPTH]
int ***p3DArray;

p3DArray = new int**[HEIGHT];
for (int i = 0; i < HEIGHT; i++) {
p3DArray[i] = new int*[WIDTH];
for (int j = 0; j < WIDTH；j++) {
p3DArray[i][j] = new int[DEPTH];
}
}

for (int i = 0; i < HEIGHT; ++i) {
for (int j = 0; j < WIDTH; ++j) {
delete [] p3DArray[i][j];
}
delete [] p3DArray[i];
}
delete [] p3DArray;
}

## 参考

http://www.cplusplus.com/forum/articles/7459/