首页 > 解决方案 > 如何在使用 2d 数组作为参数的同时创建 3d 数组

问题描述

    int main()
    {
      cout << "Welcome to the Maze Game!\n";
      cout << "(press ENTER to start)";
      cin.get();
      system("cls"); 
      bool maze[ROWS][COLS];
      loadMaze(maze);

      bool map[ROWS][COLS] = {};
      int 
        x = 0, 
        y = 0;

      map[y][x] = 1;

     printMap(map, x, y);

功能定义

我如何定义以便将地图用作参数。

void printMap(bool array[][ROWS][COLS])
{    
        // DECIDE BETWEEN:
        // print '.' if the user has been at spot before
        // print 'x' character (NOT the value of variable x) to MARK the user's current position
        // print '#' for spots the user has NOT been yet

}

标签: c++arrays

解决方案


您可以传递mapprintMap

void printMap(bool map [ROWS][COLS])

或者

void printMap(bool map [][COLS])

如果你想从你的 2D 中创建一个 3D 数组map,你可以定义一个新的 3D 数组printMap并使用来自map. 但是,我强烈建议您使用std::vector原始数组而不是原始数组,因为它们更容易使用。如果你有兴趣,这里有一些关于std::vector.


推荐阅读