如何创建多维、动态定义的数组?

发布于 2024-11-03 08:14:25 字数 406 浏览 1 评论 0原文

我知道如何创建一个单维、动态定义的数组,

 string *names = new string[number]; //number specified by the user

但是,当我尝试使其成为多维数组时,

 string *names = new string[number][number]; //doesn't work

它不起作用。什么给了?我发现这个http://www.cplusplus.com/forum/beginner/63/ 但我对他们所说的话感到完全困惑。有人愿意解释一下吗?非常感谢。

i know how to create a single-dimensional, dynamically defined array

 string *names = new string[number]; //number specified by the user

however, when i try to make it multidimensional

 string *names = new string[number][number]; //doesn't work

it doesn't work. what gives? i found this http://www.cplusplus.com/forum/beginner/63/ but am completely confused by what they are saying. anyone care to explain? thanks so much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅忆 2024-11-10 08:14:25

我试图从您的链接中提供一些解释:

// To dynamically allocate two-dimensional array we will allocate array of pointers to int first. 
// Each pointer will represent a row in your matrix.
// Next step we allocate enough memory for each row and assign this memory for row pointers.

const int rows = 4; //number of rows in your matrix
const int cols = 4; //number of columns in your matrix

// declaration
int ** a; 

/* allocating memory for pointers to rows. Each row will be a dynamically allocated array of int */
a = new int*[rows];
/* for each row you allocate enough memory to contain cols elements. cols - number of columns*/ 
for(int i = 0; i < rows; i++)
   a[i] = new int[cols];

I tried to provide some explanations to example from your link:

// To dynamically allocate two-dimensional array we will allocate array of pointers to int first. 
// Each pointer will represent a row in your matrix.
// Next step we allocate enough memory for each row and assign this memory for row pointers.

const int rows = 4; //number of rows in your matrix
const int cols = 4; //number of columns in your matrix

// declaration
int ** a; 

/* allocating memory for pointers to rows. Each row will be a dynamically allocated array of int */
a = new int*[rows];
/* for each row you allocate enough memory to contain cols elements. cols - number of columns*/ 
for(int i = 0; i < rows; i++)
   a[i] = new int[cols];
只是一片海 2024-11-10 08:14:25

一维数组(假设有三个元素)的内存布局看起来像,

names ------> [0] [1] [2] 

而二维数组(假设有 3 X 3 个元素)看起来像,

names ------> [0] --> [0] [1] [2] 
              [1] --> [0] [1] [2]
              [2] --> [0] [1] [2]
               ^
               |
           this is an array of pointers

即二维数组是指向数组的指针数组,因此你首先需要**名字。

string **names = new string*[number]; // allocating space for array of string pointers

现在,您希望该字符串指针数组的每个元素都指向一个字符串数组。

因此,您需要这样做

for(int i = 0; i < number, i++) {
   names[i] = new string[number];
}

,我希望这有助于更好地理解它。

The memory layout of single-dimension array (let's say with three elements) looks like,

names ------> [0] [1] [2] 

And the two-dimensional array (let's say with 3 X 3 elements) would look like,

names ------> [0] --> [0] [1] [2] 
              [1] --> [0] [1] [2]
              [2] --> [0] [1] [2]
               ^
               |
           this is an array of pointers

i.e. two dimensional array is array of pointers to arrays, therefore you would need **names in first place.

string **names = new string*[number]; // allocating space for array of string pointers

Now, you want each element of this array of string pointers to point to an array of strings.

Therefore, you would need to do

for(int i = 0; i < number, i++) {
   names[i] = new string[number];
}

I hope this helps to understand it better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文