c++ - 将在c++中输出多维数组内容的for循环的结构是什么

标签 c++

我需要看一个示例,说明如何输出多维数组。

string** row = new string*[level];
for(int i = 0; i < level; ++i) {
      row[i] = new string[level];
}

// outputting:

int x; // filled with some value

int y; // filled with some value

我将如何打印 row[y][x] 通过遍历 y 然后 x

最佳答案

首先,您可能应该考虑使用 std::vector 而不是手动动态分配,因为您使用的是 C++:

std::vector<std::vector<std::string>> rows(level);

代替

string** row = new string*[level];

并以这种方式初始化它:

for (std::vector<std::string>& row_vec : rows)
{
    row_vec.resize(level);
}

并迭代它只需使用嵌套的 for 循环:

for (uint32_t x(0); x < level; ++x)
{
    for (uint32_t y(0); y < level; ++y)
    {
        std::cout << "rows[" << x << "][" << y << "] = " << rows[x][y] << std::endl;
    }
}

关于c++ - 将在c++中输出多维数组内容的for循环的结构是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55545041/

相关文章:

c++ - 从 MAT 文件中读取 C 应用程序中的自定义类

c++ - 传递这个双指针并获取值有什么问题?

C++ 在 VS 中为 'better' Release模式构建定义

c++ - 在C++中使用Bitset将16位显示为四组4位

c++ - boost::thread 没有所有的 boost ?

android - 将 Sprite 矩形旋转 90 度不符合 iOS 中的纵横比

c++ - GetLogicalDrives 加上附加信息 C++

c++ - 对容器中项目的常量正确指针访问

c# - 如何使用 DLLImport 将字符串从 C# 传递到 C++(以及从 C++ 到 C#)?

c++ - 传递数组时类中出现错误指针错误