简体   繁体   English

C ++中的多维数组和指针?

[英]Multi-dimensional array and pointers in C++?

int *x = new int[5]();

With the above mentality, how should the code be written for a 2-dimensional array - int[][] ? 基于上述思路,应该如何为二维数组int[][]编写代码?

int **x = new int[5][5] () //cannot convert from 'int (*)[5]' to 'int **'

In the first statement I can use: 在第一条语句中,我可以使用:

x[0]= 1;

But the second is more complex and I could not figure it out. 但是第二个更为复杂,我无法弄清楚。 Should I use something like: 我应该使用类似的东西:

x[0][1] = 1;

Or, calculate the real position then get the value for the fourth row and column 1 或者,计算实际位置,然后获取第四行和第一列的值

x[4*5+1] = 1;

I prefer doing it this way: 我更喜欢这样:

int *i = new int[5*5];

and then I just index the array by 5 * row + col . 然后我只按5 * row + col索引数组。

You can do the initializations separately: 您可以单独进行初始化:

int **x = new int*[5];
for(unsigned int i = 0; i < 5; i++)
    x[i] = new int[5];

There is no new[][] operator in C++. C ++中没有new[][]运算符。 You will first have to allocate an array of pointers to int: 您首先必须分配一个指向int的指针数组:

int **x = new int*[5];

Then iterate over that array. 然后遍历该数组。 For each element, allocate an array of ints: 为每个元素分配一个整数数组:

for (std::size_t i = 0; i < 5; ++i)
    x[i] = new int[5];

Of course, this means you will have to do the inverse when deallocating: delete[] each element, then delete[] the larger array as a whole. 当然,这意味着你将有解除分配时做逆: delete[]的每个元素,然后delete[]的较大阵列作为一个整体。

This is how you do it: 这是您的操作方式:

int (*x)[5] = new int[7][5] ;

I made the two dimensions different so that you can see which one you have to use on the lhs. 我使这两个维度不同,以便您可以看到在lhs上必须使用哪个维度。

Ff the array has predefined size you can write simply: 如果数组具有预定义的大小,则可以简单地编写:

int x[5][5];

It compiles 编译

If not why not to use a vector? 如果不是,为什么不使用向量?

There are several ways to accomplish this: 有几种方法可以实现此目的:

  • Using gcc's support for flat multidimensional arrays (TonyK's answer, the most relevant to the question IMO). 使用gcc对平面多维数组的支持(TonyK的答案,与IMO问题最相关)。 Note that you must preserve the bounds in the array's type everywhere you use it (eg all the array sizes, except possibly the first one), and that includes functions that you call, because the produced code will assume a single array. 请注意,您必须在使用数组的所有位置(例如,所有数组大小,可能除了第一个数组大小)都保留数组类型的边界,并且该边界包括您调用的函数,因为生成的代码将假定单个数组。 The allocation of $ new int [7][5] $ causes a single array to be allocated in memory. $ new int [7] [5] $的分配导致在内存中分配单个数组。 indexed by the compiler (you can easily write a little program and print the addresses of the slots to convince yourself). 由编译器索引(您可以轻松编写一个小程序并打印插槽地址以说服自己)。

  • Using arrays of pointers to arrays. 使用指向数组的指针数组。 The problem with that approach is having to allocate all the inner arrays manually (in loops). 这种方法的问题是必须手动分配所有内部数组(循环)。

  • Some people will suggest using std::vector's of std::vectors, but this is inefficient, due to the memory allocation and copying that has to occur when the vectors resize. 有人会建议使用std :: vector的std :: vector,但这效率低下,因为在矢量调整大小时必须进行内存分配和复制。

  • Boost has a more efficient version of vectors of vectors in its multi_array lib. Boost在其multi_array库中具有更有效的vectors版本。

In any case, this question is better answered here: How do I use arrays in C++? 无论如何,在这里最好回答这个问题: 如何在C ++中使用数组?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM