将一个数组内容放入另一个 2D 数组行和列中

make one arrays content into another 2d arrays rows & colums

本文关键字:数组 另一个 2D 一个      更新时间:2023-10-16

我有一个函数,它读取一个文本文件并将字符串发送到一个数组,然后从该函数中,我将该数组和元素数量发送到我的构造函数。现在我的构造函数创建了一个动态 2D 数组(我希望)。我希望从收到的数组中为我的 2d 数组行和列分配值。

这是我的构造函数。

Graph::Graph(string cities[], int n)
{
     this->nrOfCities=n;
     this->x=n;
     this->y=n;
     this->graph=new string *[x];
     for (int i = 0; i < x; i++) 
        this->graph[i] =new string[y];
     for(int i=0;i<this->x;i++)
        for(int j=0;j<this->x;j++)
           this->graph[j]=NULL;
     for(int i=0;i<=this->x;i++)//I know this last part doesn't work.
         for(int j=0;j<this->x;j++)
             this->graph[0][j+1]=cities[j];
}
感谢

任何形式的帮助。

为了制作动态 2darray,您必须尝试 s.th。 像这样:

type** arr2d;
arr2d = new type*[rows];
for(int i=0; i<rows; ++i)
  arr2d[i] = new type[cols];