samedi 6 octobre 2018

How to define a 3d vector dynamically in a class [duplicate]

This question already has an answer here:

I am creating a 3d vector for a game which has rows and columns and in each row and column there is person. This is my code.

class City {

private:
    struct person {
    int id;
    int row;
    int col;
};

public:
    int count;
    vector<vector<vector<person*>>> houses;

City(unsigned rows, unsigned cols)
{
    count = 0;
    houses.resize(rows,vector<vector<person*> >(cols,vector<person*>(0)));
}
~City() {
    vector<vector<vector<person*>>>().swap(houses);
     }
};

bool addPerson(int row, int col)
 {
    person a;
    a.row = row;
    a.col = col;

    a.person_id = count;
    count++;

    houses[row][col].push_back(&a);
    return true;
  }
  return false;
}

I don't think I am initializing this 3D vector properly. When I add a new person, it is not properly entered into the 3D vector. I see bad data in the 3D vector when I print the person's information. Please help.

Aucun commentaire:

Enregistrer un commentaire