C++ 多维数组

多维数组

多维数组是数组的数组。

要声明多维数组,请定义变量类型,指定数组名称,后跟方括号(指定主数组有多少个元素),后跟另一组方括号(指示子数组有多少个元素)。

  1. string letters[2][4];

与普通数组一样,可以在大括号内插入带有数组文字(逗号分隔的列表)的值。在多维数组中,数组文字中的每个元素都是另一个数组字面量。

  1. string letters[2][4] = {
  2. { "A", "B", "C", "D" },
  3. { "E", "F", "G", "H" }
  4. };

数组声明中的每一组方括号都会为数组添加另一个维度。像上面这样的数组称为二维数组。

数组可以有任意数量的维度。数组的维数越多,代码就越复杂。下面的数组有三个维度:

  1. string letters[2][2][2] = {
  2. {
  3. { "A", "B" },
  4. { "C", "D" }
  5. },
  6. {
  7. { "E", "F" },
  8. { "G", "H" }
  9. }
  10. };

访问多维数组的元素

要访问多维数组的元素,请在数组的每个维度中指定一个索引号。

此语句访问字母数组第一行(0)和第三列(2)中元素的值。

  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. string letters[2][4] = {
  5. { "A", "B", "C", "D" },
  6. { "E", "F", "G", "H" }
  7. };
  8. cout << letters[0][2];
  9. return 0;
  10. }
请记住:数组索引以 0 开头:[0] 是第一个元素。[1] 是第二个元素,等等。

更改多维数组中的元素

要更改元素的值,请引用每个维度中元素的索引

  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. string letters[2][4] = {
  5. { "A", "B", "C", "D" },
  6. { "E", "F", "G", "H" }
  7. };
  8. letters[0][0] = "Z";
  9. cout << letters[0][0];
  10. return 0;
  11. }

在多维数组中循环

要在多维数组中循环,数组的每个维度都需要一个循环。

以下实例输出字母数组中的所有元素:

  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. string letters[2][4] = {
  5. { "A", "B", "C", "D" },
  6. { "E", "F", "G", "H" }
  7. };
  8. for(int i = 0; i < 2; i++) {
  9. for(int j = 0; j < 4; j++) {
  10. cout << letters[i][j] << "\n";
  11. }
  12. }
  13. return 0;
  14. }

这个例子展示了如何在三维数组中循环:

  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. string letters[2][2][2] = {
  5. {
  6. { "A", "B" },
  7. { "C", "D" }
  8. },
  9. {
  10. { "E", "F" },
  11. { "G", "H" }
  12. }
  13. };
  14. for(int i = 0; i < 2; i++) {
  15. for(int j = 0; j < 2; j++) {
  16. for(int k = 0; k < 2; k++) {
  17. cout << letters[i][j][k] << "\n";
  18. }
  19. }
  20. }
  21. return 0;
  22. }

为什么是多维数组?

多维数组非常擅长表示网格。这个例子展示了它们的实际用途。在下面的实例中,我们使用多维数组来表示战舰的一个小游戏:

  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. // We put "1" to indicate there is a ship.
  5. bool ships[4][4] = {
  6. { 0, 1, 1, 0 },
  7. { 0, 0, 0, 0 },
  8. { 0, 0, 1, 0 },
  9. { 0, 0, 1, 0 }
  10. };
  11. // Keep track of how many hits the player has and how many turns they have played in these variables
  12. int hits = 0;
  13. int numberOfTurns = 0;
  14. // Allow the player to keep going until they have hit all four ships
  15. while (hits < 4) {
  16. int row, column;
  17. cout << "Selecting coordinates\n";
  18. // Ask the player for a row
  19. cout << "Choose a row number between 0 and 3: ";
  20. cin >> row;
  21. // Ask the player for a column
  22. cout << "Choose a column number between 0 and 3: ";
  23. cin >> column;
  24. // Check if a ship exists in those coordinates
  25. if (ships[row][column]) {
  26. // If the player hit a ship, remove it by setting the value to zero.
  27. ships[row][column] = 0;
  28. // Increase the hit counter
  29. hits++;
  30. // Tell the player that they have hit a ship and how many ships are left
  31. cout << "Hit! " << (4-hits) << " left.\n\n";
  32. } else {
  33. // Tell the player that they missed
  34. cout << "Miss\n\n";
  35. }
  36. // Count how many turns the player has taken
  37. numberOfTurns++;
  38. }
  39. cout << "Victory!\n";
  40. cout << "You won in " << numberOfTurns << " turns";
  41. return 0;
  42. }