How do I print contents of a vector<vector<Tiles>> where tiles is an object

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>>
Last edited on
What is the issue? i.e., what error are you getting? Does getSpace() return something cout can display?
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
Last edited on
Line 23 refers to vDungeon, but there is no such thing in scope. Lets assume that is a typo.

You add 20 items into vDungeonRows, but you attempt to access 79.

In this case range-based loops would do:
1
2
3
4
5
6
7
for ( const vector<Tile> & row : vDungeonRows )
{
  for ( const Tile & tile : row )
    {
      std::cout << tile.getSpace();
    }
}

@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 

Topic archived. No new replies allowed.