Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将多维数组分配给结构? 我是C++的新手,我想为程序分配一个二维数组指针。代码是 struct normal { double n_x, n_y; };_C++_Multidimensional Array_Struct - Fatal编程技术网

如何将多维数组分配给结构? 我是C++的新手,我想为程序分配一个二维数组指针。代码是 struct normal { double n_x, n_y; };

如何将多维数组分配给结构? 我是C++的新手,我想为程序分配一个二维数组指针。代码是 struct normal { double n_x, n_y; };,c++,multidimensional-array,struct,C++,Multidimensional Array,Struct,这是我定义的结构,我想在main中分配一个2d数组指针,如下所示: normal **normal_cell; *normal_cell = new normal[p]; for(int i=0;i<p;i++) { normal_cell[i] = new normal[4]; } normal**normal\u单元; *正常细胞=新正常细胞[p]; 对于(int i=0;i < p>),只使用< C++ > NealthyCys=新的正常*[P] < /C> >而不是 *Q

这是我定义的结构,我想在main中分配一个2d数组指针,如下所示:

normal **normal_cell;
*normal_cell = new normal[p];
for(int i=0;i<p;i++)
{
    normal_cell[i] = new normal[4];
}
normal**normal\u单元;
*正常细胞=新正常细胞[p];

对于(int i=0;i < p>),只使用< C++ > NealthyCys=新的正常*[P] < /C> >而不是<代码> *QualthCys=新的常态[P] < /> >/P>> P> C++,我们使用向量代替数组和指针:

#include<vector>
using std:: vector;
struct normal
{
    double n_x, n_y;
};

vector<vector<normal>> normal_cell;
void foo() {
    int p=10;
    normal_cell.resize(p);
    for(int i=0;i<p;i++)
    {
        normal_cell.at(i).resize(4);
    }
}
#包括
使用std::vector;
结构正常
{
双n_x,n_y;
};
载体正常细胞;
void foo(){
int p=10;
正常细胞大小(p);
对于(int i=0;i只需尝试一下:

    //if normal_cell[4][5]
    const int m = 4; //row
    const int n = 5; //column
    normal **normal_cell = new normal*[m]; //new row
    for(int i=0; i<m; ++i)
        normal_cell[i] = new normal[n]; //new column
//如果单元[4][5]正常
常量int m=4;//行
常量int n=5;//列
法线**法线_单元格=新法线*[m];//新行

对于(int i=0;iI已尝试编译,但出现错误:'p'未在此范围内声明normal\u cell=new normal*[p];然后在分配normal\u cell之前定义p,例如
int p=10;