将动态2d数组中的所有值设置为一个集合值。

时间:2021-11-08 17:09:25

I have to setup a dynamic 2 D array that is x Number of rows by y number of columns. This array has been declared as an enumerated type and I want to initialize all values to a known value of EMPTY. Is there a way to do this with a single command or do I need to loop through each row/column abd set it up that way. I know static arrays can be done as Array[][] = {EMPTY} but I cannot get this method to work.

我需要建立一个动态的二维数组,它是x行数乘以y的列数。该数组已声明为枚举类型,我希望将所有值初始化为空的已知值。是否有一种方法可以用单个命令来完成,或者我需要循环遍历每个行/列abd,以这样的方式设置它。我知道静态数组可以作为数组[][]= {EMPTY}来完成,但是我不能让这个方法工作。

status ** seats;

seats = new status* [NO_OF_ROWS];// declare array with NO_OF_ROWS
for (row = 0; row < NO_OF_ROWS; row++)
   seats[row] = new status[NO_OF_SEATS]; 

2 个解决方案

#1


3  

Yes:

是的:

#include <vector>

typedef std::vector< std::vector<status> > seat_type;

seat_type seats(NO_OF_ROWS, std::vector<status>(NO_OF_SEATS));

// now use seats[i][j] etc.

In C++, a "dynamic array" is called std::vector.

在c++中,“动态数组”被称为std::vector。


If you want to avoid many dynamic allocations, you could alternatively make a single vector

如果你想避免许多动态分配,你可以选择一个单独的向量。

std::vector<status> seats(NO_OF_ROWS * NO_OF_SEATS);

and access it in strides, i.e. the (i,j)th seat is seats[j + NO_OF_SEATS * i].

并且在大踏步前进,即(i,j)座位是座位[j + no_of_seat * i]。

#2


0  

You could try using memset - as you know, arrays in C are internally defined as a contiguous memory block. memset, on the other hand, is a function which fills a certain memory block with a certain value.

您可以尝试使用memset——您知道,C中的数组在内部被定义为一个连续的内存块。另一方面,memset是一个函数,它以一定的值填充某个内存块。

Now, since your bi-dimensional array is not contiguous (because you have an array of pointers), you won't be able to call 'memset' just once, but at least you can call it for every row, meaning you'll call it NO_OF_ROWS times.

现在,由于您的双维数组不是连续的(因为您有一个指针数组),您不能只调用一次“memset”,但是至少您可以为每一行调用它,这意味着您将调用它NO_OF_ROWS时间。

It should be similar to this (note that some casting may be required, and also my c++ is a little rusty):

它应该类似于这个(注意,可能需要一些转换,而且我的c++有点生锈):

for (row = 0; row < NO_OF_ROWS; row++)
{
    seats[row] = new status[NO_OF_SEATS]; 
    memset(seats[row],EMPTY,NO_OF_SEATS*sizeof(status));
}

#1


3  

Yes:

是的:

#include <vector>

typedef std::vector< std::vector<status> > seat_type;

seat_type seats(NO_OF_ROWS, std::vector<status>(NO_OF_SEATS));

// now use seats[i][j] etc.

In C++, a "dynamic array" is called std::vector.

在c++中,“动态数组”被称为std::vector。


If you want to avoid many dynamic allocations, you could alternatively make a single vector

如果你想避免许多动态分配,你可以选择一个单独的向量。

std::vector<status> seats(NO_OF_ROWS * NO_OF_SEATS);

and access it in strides, i.e. the (i,j)th seat is seats[j + NO_OF_SEATS * i].

并且在大踏步前进,即(i,j)座位是座位[j + no_of_seat * i]。

#2


0  

You could try using memset - as you know, arrays in C are internally defined as a contiguous memory block. memset, on the other hand, is a function which fills a certain memory block with a certain value.

您可以尝试使用memset——您知道,C中的数组在内部被定义为一个连续的内存块。另一方面,memset是一个函数,它以一定的值填充某个内存块。

Now, since your bi-dimensional array is not contiguous (because you have an array of pointers), you won't be able to call 'memset' just once, but at least you can call it for every row, meaning you'll call it NO_OF_ROWS times.

现在,由于您的双维数组不是连续的(因为您有一个指针数组),您不能只调用一次“memset”,但是至少您可以为每一行调用它,这意味着您将调用它NO_OF_ROWS时间。

It should be similar to this (note that some casting may be required, and also my c++ is a little rusty):

它应该类似于这个(注意,可能需要一些转换,而且我的c++有点生锈):

for (row = 0; row < NO_OF_ROWS; row++)
{
    seats[row] = new status[NO_OF_SEATS]; 
    memset(seats[row],EMPTY,NO_OF_SEATS*sizeof(status));
}