填写含有scruct的2d载体

时间:2018-06-02 10:20:29

标签: c++ multidimensional-array struct fill

我有一个小结构包含bool,char&两个整数。但是当我尝试运行此代码时,我遇到了一个问题。

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);
}

我想填充我的2D数组,以便以后可以用以下方式编写内容:

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

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

    }

}

然而,截至目前,我收到错误: “temp没有成员push_back()”。

6 个答案:

答案 0 :(得分:2)

您指的是temp[i]。由于temp不是数组或矢量矢量,因此不能像这样使用。当您执行temp[i]时,您将获得向量临时的i'th元素,即Struct,其中没有push_back方法。

您可以执行以下操作来初始化Matrix

....
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);
}
....

答案 1 :(得分:1)

正如上面的答案已经提到过,你试图在push_back()类型而不是Struct类型上调用std::vector<Struct>,所以你的编译器抱怨相同。请尝试以下:

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);
    }
}

答案 2 :(得分:0)

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

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 
}

答案 3 :(得分:0)

使用此类机制制作结构的2D数组。

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);

答案 4 :(得分:0)

您可以简单地将resize()与默认元素的默认行向量一起使用:

matrix.resize(rows,·vector<Struct>(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));
}

答案 5 :(得分:0)

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

    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 */
    }

在一个简短的例子中,你可以做类似的事情:

#include <iostream>
#include <vector>

#define ROWS  10
#define COLS  ROWS

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

int main (void) {

    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 */
    }

    for (auto& i : matrix) {    /* auto-range loop over matrix  */
        for (auto& j : i)       /* auto-range loop over vectors */
            std::cout << j.x << ',' << j.y << "  check: " << j.check <<
                    "  display: '" << j.display << "'\n";
        std::cout << '\n';  /* tidy up with newline between vectors */
    }
}

示例使用/输出

$ ./bin/vect2d_struct
0,0  check: 1  display: '*'
0,1  check: 1  display: '*'
0,2  check: 1  display: '*'
0,3  check: 1  display: '*'
0,4  check: 1  display: '*'
0,5  check: 1  display: '*'
0,6  check: 1  display: '*'
0,7  check: 1  display: '*'
0,8  check: 1  display: '*'
0,9  check: 1  display: '*'

1,0  check: 1  display: '*'
1,1  check: 1  display: '*'
1,2  check: 1  display: '*'
1,3  check: 1  display: '*'
1,4  check: 1  display: '*'
... <snip>
8,5  check: 1  display: '*'
8,6  check: 1  display: '*'
8,7  check: 1  display: '*'
8,8  check: 1  display: '*'
8,9  check: 1  display: '*'

9,0  check: 1  display: '*'
9,1  check: 1  display: '*'
9,2  check: 1  display: '*'
9,3  check: 1  display: '*'
9,4  check: 1  display: '*'
9,5  check: 1  display: '*'
9,6  check: 1  display: '*'
9,7  check: 1  display: '*'
9,8  check: 1  display: '*'
9,9  check: 1  display: '*'

仔细看看并告诉我这是否是您的意图,如果您有任何其他问题。