How do you delete 2d vectors?
I tried deleting a 2d vector by using .clear(), but when I tried reloading the Game object, it prints out really weirdly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
Game::Game(const std::string &filename) //loads txt from filename and creates a map
{
std::ifstream fs;
fs.open(filename);
//checks to see if file exists
if (fs.fail())
{
std::cerr << "Opening file failed.\n";
}
//inputs map into vector
short r,c;
fs >> c;
fs >> r;
fs >> std::noskipws;
fs.ignore(1);
row = r;
col = c;
map2d.resize(row, std::vector<char>(col));
for (short i = 0; i < row; i++)
{
for (short j = 0; j < col; j++)
{
char ch;
fs >> ch;
map2d[i][j] = ch;
}
fs.ignore(1);
}
map2d[1][1] = '@';
short total_bountyy = 0;
for (short i = 0; i < row; i++)
{
for (short j = 0; j < col; j++)
{
if (map2d[i][j] == '$')
{
total_bountyy = total_bountyy + 1;
}
}
}
total_bounty = total_bountyy;
fs.close(); //finished
}
|
attempt to clear:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
void Game::make_move()
{
bounties_remaining();
if (bounty_remaining == 0)
{
Display();
std::cout << "\nGame over, You win!\n\n";
map2d.clear();
map2d.resize(0);
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
std::cout << map2d[i][j];
}
}
// Game();
// Game("map.dat");
Menu();
return;
}
|
Last edited on
If map2d is a vector of vectors, then
1 2
|
map2d.clear();
map2d.resize(0);
|
will resize it to size 0. In what way does it print out weirdly?
1 2 3 4 5 6 7 8 9
|
map2d.clear();
map2d.resize(0);
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
std::cout << map2d[i][j];
}
}
|
I see here you are reading values from map2d that don't exist anymore. That's a bad idea. You shouldn't read values that don't exist anymore.
Topic archived. No new replies allowed.