c++ - 如何创建二维数组 C++?

标签 c++ arrays 2d

我需要在 C++ 中创建二维数组。

我不能通过 int mas= new int[x][y];auto mas= new int[x][y]; 我需要像这样动态地创建一个数组:

int x,y
auto mas= new int[x][y];//error - must be const.

请帮帮我。

最佳答案

int x,y;
x =3;
y = 5;
int ** mas = new int*[x];
for (int i=0;i<x;i++)
{
   mas[i] = new int[y];
}

我觉得是这样的。 不要忘记

for(int i=0;i<x;i++)
   delete[] mas[i];
delete[] mas;

最后。

关于c++ - 如何创建二维数组 C++?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7944154/

相关文章:

c++ - 临时 const 数组未绑定(bind)到右值引用

c++ - bad_weak_ptr with boost 智能指针

python - 成对平方差的高效 Numpy 计算

unity-game-engine - 使两个物理对象不发生碰撞,但在 Unity 中检测碰撞

c++ - 在 C++ 中不在整个 std::string 中打印引号给出负 ASCII 值?

c++ - 指向类方法的指针

PHP:如何将数组值插入到mysql语句中

ruby-on-rails - 如何遍历 JSON 数组以获取键的值?

java - 为什么我无法打印二维数组?

C++控制台游戏,故障重新切换语句以在二维数组中移动字符