How would i print the contents of a vector<vector<Tile> where Tile is a pointer to an object?
this is my code up to the point where im having the issue.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
vector< vector<Tile> > vDungeonRows;
for(int j=0; j<20; j++)
{
vector<Tile> vDungeon;
for(int i=0; i<79; i++)
{
Tile newTile;
vDungeon.push_back(newTile);
}
vDungeonRows.push_back(vDungeon);
}
// i know i would need a nested for loop but im not exactly sure how
// i would access the elements of the vector<vector<Tile>>
the issue is what should i do in order to access the objects in vDungeon which is several vectors inside the vector vDungeonRows so i can print them. the cout statement in the second nested for loop does not work that was just one of my attempts to access the Objects. and getSpace is just an accessor function that is in my Tile.h file that just gets the value of space which is ' ' an empty char
@keskiverto i edited my code. that second nested for loop was a failed attempt to try and access each indivdual Tile object. i dont see how that access' the Tile in vDungeon though. wouldnt i need to the 79 is to access the 79 Tiles that are in each of the 20 rows
You don't want to write literal 79 in your code at this point. You want to operate based on the actual size of the vector. The range-based for is in C++11. Iterators or indices work too:
1 2 3 4 5 6 7 8 9 10 11 12
for ( vector< vector<Tile> >::size_type row = 0; row < vDungeonRows.size(); ++row )
{
for ( vector< vector<Tile> >::value_type::size_type col = 0; col < vDungeonRows[row].size(); ++col )
{
vDungeonRows[row][col].getSpace();
// same as (vDungeonRows[row])[col].getSpace();
}
}
// The vector<vector<Tile>>::value_type is vector<Tile>
// vector::operator[] returns essentially value_type&
// Thus, in this case vDungeonRows[row] is a reference to a vector<Tile> object
// Similarly, operator[] for a vector<Tile> object returns a reference to a Tile object