C++多维数组
一维数组
int ia[] = {0,1,2,3,4,5,6,7,8,9}; //ia是数组
多维数组
C++ 中并没有多维数组,它就是数组的数组。
int ia[3][4];
初始化
int ia[3][4] = {
{0, 1, 2, 3},
{4, 5, 6, 7},
{8, 9, 10, 11}
};
//等价于
int ia[3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
//显示初始化每行的首元素
int ia[3][4] = { {0} , {4} , {8}};
//显示初始化第一行
int ix[3][4] = {0, 3, 6, 9};
引用
下标引用
通过ia[i][j]
方式引用下标:
constexpr size_t rowCnt = 3, colCnt = 4;
int ia[rowCnt][colCnt];
for (size_t i = 0; i != rowCnt; i++) {
for (size_t j = 0; j != colCnt; j++) {
ia[i][j] = i * colCnt + j;
}
}
指针引用
Point root_points[2][4];
root_points[0][0] = Point(215,220);
root_points[0][1] = Point(460,225);
root_points[0][2] = Point(466,450);
root_points[0][3] = Point(235,465);
root_points[1][0] = Point(800,800);
root_points[1][1] = Point(800,800);
root_points[1][2] = Point(800,800);
root_points[1][3] = Point(800,800);
//Point* ppt[1] = {root_points[0]}; //此时ppt[1]是第一行数据的首地址,为215,220
Point* ppt[1] = {root_points[1]}; //此时ppt[1]是第二行数据的首地址,为800,800