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

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

问题描述

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

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

我不能做到这一点 INT MAS =新INT [X] [Y]; 自动MAS =新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];

然后访问值:

Then to access a value:

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天全站免登陆