C++ 填充包含scruct的二维向量

C++ 填充包含scruct的二维向量,c++,multidimensional-array,struct,fill,C++,Multidimensional Array,Struct,Fill,我有一个包含bool、char和两个int的小结构。但是当我试图运行这段代码时,我遇到了一个问题 struct Struct { bool check; char display; int x,y; }; typedef vector<Struct> Array; vector<Array> Matrix; for (int i = 0; i < rows; i++) { vector<Struct> temp; fo

我有一个包含bool、char和两个int的小结构。但是当我试图运行这段代码时,我遇到了一个问题

struct Struct
{
  bool check;
  char display;
  int x,y;
};

typedef vector<Struct> Array;

vector<Array> Matrix;

for (int i = 0; i < rows; i++)
{
    vector<Struct> temp;

    for (int j = 0; j < cols; j++)
    {
        temp[i].push_back(Matrix);
    }

    Matrix.push_back(temp);
}
struct结构
{
布尔检查;
字符显示;
int x,y;
};
typedef矢量阵列;
向量矩阵;
对于(int i=0;i
我想填充我的2D数组,以便以后能够编写以下内容:

for (int i = 0; i < rows; i++)
{
    for (int j = 0; j < cols; j++)
    {

            Matrix[i][j].display = '*';

    }

}
for(int i=0;i
但是,到目前为止,我得到了一个错误:
“temp没有成员push_back()”

您指的是
temp[i]
。由于temp不是一个数组或向量向量,所以不能这样使用。当您执行
temp[i]
时,您将获得向量temp的
i'th
元素,它是一个
Struct
,没有
push_-back
方法

您可以这样做来初始化
矩阵

....
for (int i = 0; i < rows; i++)
{
    vector<Struct> temp;

    for (int j = 0; j < cols; j++)
    {
        Struct s;
        s.check = true;
        s.display = 'c';
        s.x = i;
        s.y = j;  
        temp.push_back(s);
    }

    Matrix.push_back(temp);
}
....
。。。。
对于(int i=0;i
您指的是
temp[i]
。由于temp不是一个数组或向量向量,所以不能这样使用。当您执行
temp[i]
时,您将获得向量temp的
i'th
元素,它是一个
Struct
,没有
push_-back
方法

您可以这样做来初始化
矩阵

....
for (int i = 0; i < rows; i++)
{
    vector<Struct> temp;

    for (int j = 0; j < cols; j++)
    {
        Struct s;
        s.check = true;
        s.display = 'c';
        s.x = i;
        s.y = j;  
        temp.push_back(s);
    }

    Matrix.push_back(temp);
}
....
。。。。
对于(int i=0;i
您正在混合类型,编译器发出错误:

typedef vector<Struct> Array; 

vector<Array> Matrix; // a vector of vectors

for (int i = 0; i < rows; i++) {
    vector<Struct> temp;

    for (int j = 0; j < cols; j++)
    {
       // temp[i].push_back(Matrix); -> wrong  
       // Matrix is a vector of vectors.
       // but temp is a vector of structs.
       temp.push_back(Struct()); 
    }

    Matrix.push_back(temp); //push the vector into the vector 
}
typedef向量数组;
向量矩阵;//向量的向量
对于(int i=0;i错误
//矩阵是向量的向量。
//但是temp是一个结构向量。
临时推回(Struct());
}
Matrix.push_back(temp);//将向量推入向量
}

您正在混合类型,编译器发出错误:

typedef vector<Struct> Array; 

vector<Array> Matrix; // a vector of vectors

for (int i = 0; i < rows; i++) {
    vector<Struct> temp;

    for (int j = 0; j < cols; j++)
    {
       // temp[i].push_back(Matrix); -> wrong  
       // Matrix is a vector of vectors.
       // but temp is a vector of structs.
       temp.push_back(Struct()); 
    }

    Matrix.push_back(temp); //push the vector into the vector 
}
typedef向量数组;
向量矩阵;//向量的向量
对于(int i=0;i错误
//矩阵是向量的向量。
//但是temp是一个结构向量。
临时推回(Struct());
}
Matrix.push_back(temp);//将向量推入向量
}

如上所述,您试图在
Struct
类型上调用
push_back()
,而不是在
std::vector
类型上调用,因此您的编译器也在抱怨同样的问题。请尝试以下内容:

void initMatrix(int rows, int cols)
{
    for (int i = 0; i < rows; i++)
    {
        Array temp;
        for (int j = 0; j < cols; j++)
        {
            Struct s;
            // Struct s = initStruct(); // if you want to initialize with other values
            temp.push_back(s);
        }

        Matrix.push_back(temp);
    }
}
void initMatrix(int行,int列)
{
对于(int i=0;i
如上所述,您试图在
Struct
类型上调用
push_back()
,而不是在
std::vector
类型上调用,因此您的编译器也在抱怨同样的问题。请尝试以下内容:

void initMatrix(int rows, int cols)
{
    for (int i = 0; i < rows; i++)
    {
        Array temp;
        for (int j = 0; j < cols; j++)
        {
            Struct s;
            // Struct s = initStruct(); // if you want to initialize with other values
            temp.push_back(s);
        }

        Matrix.push_back(temp);
    }
}
void initMatrix(int行,int列)
{
对于(int i=0;i
使用这种类型的机制来制作结构的二维数组

vector < vector<Struct>> xyz;
vector<Struct> abc;

    //set value ++++++++
    Struct _st;
    _st.check = true;
    _st.display = NULL;
    _st.x = 10;
    _st.y = 10;
    //set value --------

abc.push_back(_st);
xyz.push_back(abc);
vectorxyz;
矢量abc;
//设定值++++++++
结构;
_st.check=true;
_st.display=NULL;
_st.x=10;
_st.y=10;
//设定值--------
abc.向后推(st);
xyz.推回(abc);

使用这种类型的机制来制作结构的二维数组

vector < vector<Struct>> xyz;
vector<Struct> abc;

    //set value ++++++++
    Struct _st;
    _st.check = true;
    _st.display = NULL;
    _st.x = 10;
    _st.y = 10;
    //set value --------

abc.push_back(_st);
xyz.push_back(abc);
vectorxyz;
矢量abc;
//设定值++++++++
结构;
_st.check=true;
_st.display=NULL;
_st.x=10;
_st.y=10;
//设定值--------
abc.向后推(st);
xyz.推回(abc);
您只需将resize()与默认元素的默认行向量一起使用即可:

matrix.resize(rows,·vector<Struct>(cols));
matrix.resize(行,·向量(cols));
下面是C++11中的一个示例,在resize()之前使用clear(),以便使用已经使用过的矩阵的默认值重新调整它

#include <vector>

using namespace std;

struct Struct {
    bool check = false;
    char display = '\0';
    int x = 0, y = 0;
};

void init_matrix(vector<vector<Struct>>& matrix, size_t rows, size_t cols)
{
    matrix.clear();
    matrix.resize(rows, vector<Struct>(cols));
}
#包括
使用名称空间std;
结构{
布尔检查=假;
字符显示='\0';
int x=0,y=0;
};
void init_矩阵(向量和矩阵、大小行、大小列)
{
matrix.clear();
调整大小(行、向量(cols));
}
您只需将resize()与默认元素的默认行向量一起使用即可:

matrix.resize(rows,·vector<Struct>(cols));
matrix.resize(行,·向量(cols));
下面是C++11中的一个示例,在resize()之前使用clear(),以便使用已经使用过的矩阵的默认值重新调整它

#include <vector>

using namespace std;

struct Struct {
    bool check = false;
    char display = '\0';
    int x = 0, y = 0;
};

void init_matrix(vector<vector<Struct>>& matrix, size_t rows, size_t cols)
{
    matrix.clear();
    matrix.resize(rows, vector<Struct>(cols));
}
#包括
使用名称空间std;
结构{
布尔检查=假;
字符显示='\0';
int x=0,y=0;
};
void init_矩阵(向量和矩阵、大小行、大小列)
{
matrix.clear();
调整大小(行、向量(cols));
}

为了简化数据处理,您还可以通过初始化结构来分配值,而不是一次分配一个成员,例如

    std::vector<std::vector<Struct>> matrix;    /* declare matrix */

    for (int i = 0; i < ROWS; i++) {        /* loop ROWS times */
        std::vector<Struct> v;              /* declare vector of Struct */
        for (int j = 0; j < COLS; j++) {    /* loop COLS times */
            Struct s = { 1, '*', i, j };    /* initialize struct */
            v.push_back (s);                /* push_back to vector */
        }
        matrix.push_back(v);    /* push back to matrix */
    }
std::向量矩阵;/*声明矩阵*/
对于(int i=0;i<行;i++