如何创建多维、动态定义的数组?
我知道如何创建一个单维、动态定义的数组,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我试图从您的链接中提供一些解释:
I tried to provide some explanations to example from your link:
一维数组(假设有三个元素)的内存布局看起来像,
而二维数组(假设有 3 X 3 个元素)看起来像,
即二维数组是指向数组的指针数组,因此你首先需要**名字。
现在,您希望该字符串指针数组的每个元素都指向一个字符串数组。
因此,您需要这样做
,我希望这有助于更好地理解它。
The memory layout of single-dimension array (let's say with three elements) looks like,
And the two-dimensional array (let's say with 3 X 3 elements) would look like,
i.e. two dimensional array is array of pointers to arrays, therefore you would need **names in first place.
Now, you want each element of this array of string pointers to point to an array of strings.
Therefore, you would need to do
I hope this helps to understand it better.