本 Wiki 开启了 HTTPS。但由于同 IP 的 Blog 也开启了 HTTPS,因此本站必须要支持 SNI 的浏览器才能浏览。为了兼容一部分浏览器,本站保留了 HTTP 作为兼容。如果您的浏览器支持 SNI,请尽量通过 HTTPS 访问本站,谢谢!
这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录前一修订版后一修订版 | 前一修订版后一修订版两侧同时换到之后的修订记录 | ||
cs:programming:cpp:courses:cpp_basic_deep:chpt_3 [2024/04/17 23:51] – [多维数组的初始化] codinghare | cs:programming:cpp:courses:cpp_basic_deep:chpt_3 [2024/04/18 03:31] – [多维数组的遍历] codinghare | ||
---|---|---|---|
行 305: | 行 305: | ||
int a[][4] = {1,2,3,4}; // int[1][4] | int a[][4] = {1,2,3,4}; // int[1][4] | ||
</ | </ | ||
- | ===多维数组的索引和遍历=== | + | ===多维数组的遍历=== |
+ | * 多维数组遍历需要多重循环 | ||
+ | <code cpp> | ||
+ | int x [3][4] = {{1,2,3,4}, {5,6,7,8}, {9, | ||
+ | |||
+ | //range for | ||
+ | // | ||
+ | // | ||
+ | // | ||
+ | // | ||
+ | for (auto &row : x) | ||
+ | { | ||
+ | for (auto col : row) | ||
+ | { | ||
+ | std::cout << col << " "; | ||
+ | } | ||
+ | std::cout << std:: | ||
+ | } | ||
+ | |||
+ | //while loop | ||
+ | //注意 std::size() 的参数不同 | ||
+ | //遍历的顺序:x[0][0-3], | ||
+ | // | ||
+ | size_t outter = 0; | ||
+ | while (outter < std:: | ||
+ | { | ||
+ | size_t inner = 0; | ||
+ | while (inner < std:: | ||
+ | { | ||
+ | std::cout << x[outter][inner] <<" | ||
+ | inner++; | ||
+ | } | ||
+ | outter++; | ||
+ | std::cout << std::endl; | ||
+ | } | ||
+ | // | ||
+ | // | ||
+ | auto OutterP = std:: | ||
+ | while (OutterP != std:: | ||
+ | { | ||
+ | auto InnerP = std:: | ||
+ | { | ||
+ | while(InnerP != std:: | ||
+ | { | ||
+ | std::cout << *InnerP << " "; | ||
+ | InnerP++; | ||
+ | } | ||
+ | std::cout << std:: | ||
+ | } | ||
+ | OutterP++; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===多维数组与指针=== | ||
+ | * 多维数组也可以转化为指针,但只有最高维会进行转换 | ||
+ | * 对于外围数组,如果指针要有意义,其指向的单位一定是外围数组的元素 | ||
+ | * 比如下面的例子,外围数组有3个元素,如果移动指针 '' | ||
+ | <code cpp> | ||
+ | int x[3][4]; | ||
+ | // ptr int(*)[4] | ||
+ | auto ptr = x; | ||
+ | //ptr2 int(*)[4][5] | ||
+ | int y[3][4][5]; | ||
+ | auto ptr2 = y; | ||
+ | </ | ||
+ | ==使用类型别名声明多维数组的指针== | ||
+ | <code cpp> | ||
+ | using A2 = Int[4][5]; | ||
+ | int x[3][4][5]; | ||
+ | A2* ptr = x; //x 指向 int[4][5] | ||
+ | </ | ||
+ | 注意类型别名会改变维度的优先级: | ||
+ | <code cpp> | ||
+ | using A = int[4]; | ||
+ | // | ||
+ | A z2[3]; //等价与 int z2[4][3]; | ||
+ | </ |