简体   繁体   English

如何将数组打印到控制台?

[英]How do i print an array out to the console?

I'm currently working to make a 2D grid which the user can select the start destination, end destination and obstacles in between.我目前正在制作一个 2D 网格,用户可以将 select 作为起点、终点和两者之间的障碍物。 I would like to print the array (as shown above) to the console to help users visualise the the array they are traversing.我想将数组(如上所示)打印到控制台,以帮助用户可视化他们正在遍历的数组。 I'm using Visual Studio and working in c++.我正在使用 Visual Studio 并在 c++ 中工作。 Further to this, is there a function for the user to change particular parts of the array to 0's to create "obstacles"?除此之外,是否有 function 供用户将数组的特定部分更改为 0 以创建“障碍”?

        { { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } },
          { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } },
          { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } },
          { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } },
          { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } },
          { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } },
          { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } },
          { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } },
          { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } } }
    };

To change specific parts of an array you can access it's values like this:要更改数组的特定部分,您可以访问它的值,如下所示:

arr[4][5] = 5; // Returns the sixth element in the fifth row and changes it to 5

To print you would right something like this assuming you are working with std::vector (if not substitute arr.size() with a literal or your own variable).要打印,假设您正在使用 std::vector (如果不是用文字或您自己的变量替换 arr.size() ),您会正确地打印这样的东西。

for(int i = 0; i < arr.size(); i++){
    for(int j = 0; j < arr[i].size(); j++){
        std::cout << arr[i][j] << ", ";
    }
    std::cout << std::endl;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM