0

I have defined a struct

struct path{
    char type;
    bool visit;
    bool inPath;
    int level;
}

I have also defined a vector of vectors of this type struct

vector < vector<path> > spaceStation(numLevels*levelSize,vector<path> (levelSize));

I have two questions.

1) Have i defined the vector so that the number of rows pertain to (numLevels*levelSize) and columns pertain to levelSize

2) When accessing the individual elements of the vector, how can i set the elements of the struct inside it. I have tried using the .at() member function to little success

2 Answers 2

1

Re: 1

Yes. But I can't help feeling like you wanted to do this instead:

vector < vector<path> > spaceStation(numLevels,vector<path> (levelSize))

Note that using the term "rows" and "columns" is entirely in your imagination, concerning vectors. You just have a vector inside another vector. It's like an array of arrays - no special geometry implied.

Re: 2

Because you have a vector of vector, you need to use two indices, not just one:

spaceStation[level][pathindex].visit = true;

Where spaceStation[level] returns the vector at index level, which you then take the element at position pathindex (which is an instance of your struct), and finally modify a value in that struct.

Sign up to request clarification or add additional context in comments.

Comments

0

For Q1, you are correct. For example: a 4x4 dimension vector.

vector< vector< int > > vec(4, vector(4));

For Q2, to access the path, can't you do the following:

spaceStation[2][3] to access row 2 column 3 data, for example.

Then you can do:

spaceStation[2][3].visit to access elements inside your struct.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.