如何创建二维数组 C++? [英] How to create 2d array c++?

查看:33
本文介绍了如何创建二维数组 C++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to create 2d array in c++.

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

I can't do it by int mas= new int[x][y]; or auto mas= new int[x][y]; I need to create an array dynamically like:

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

请帮帮我.

推荐答案

我的建议是首先避免多维数组的痛苦并使用结构体.

My advice would be to avoid the pain of multidimensional arrays in the first place and use a struct.

struct Point {
    int x;
    int y;
}

int points = 10;
Point myArray[points];

然后访问一个值:

printf("x: %d, y: %d", myArray[2].x, myArray[2].y);

不过,这完全取决于您要实现的目标.

Depends on exactly what you're trying to achieve, though.

这篇关于如何创建二维数组 C++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆