samedi 6 octobre 2018

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

This question already has an answer here:

I am creating a grid with rows and columns in each row and column a person is being assigned. For this I created a 3d vector for row and column and for the person being assigned. Here is my code.

class City {

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

public:

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

City(unsigned nrows, unsigned ncols)
{
    houses.resize(nrows,vector<vector<person*> >(ncols,vector<person*>(0)));
}
void addPerson(int row, int col)
{

        person a;
        a.row = row;
        a.col = col;

        a.person_id = count;

        houses[row][col].push_back(&a);

}

I created a 3d vector dynamically inside a class. But the values which are being added to the vector are not being stored in them. How to solve this issue.

Aucun commentaire:

Enregistrer un commentaire