Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualiMalc

Posted 17 December 2012 - 01:43 AM

Here is the finished product.

vector<vector<bool>> mv;
int x = 11;
int y = 11;
for(int i=0;i<x;i++) {
  mv.push_back(vector<bool>());
  for(int j=0;j<y;j++) {
   mv[i].push_back(new bool);
  }
}

Whoa hold on a sec there. You're dynamically allocating each bool. That is not a "finished product", and is not right at all. You would simply need to push_back the value, i.e. false. It's only compiling due to a pointer being able to be implicity converted to a bool, with the value 'true' in this case since the pointer isn't NULL. So it leaks like a sieve. It's also much longer, and much less efficient in both memory and time taken*, even if you ignore the masses of leaks it causes. Nope, far from being finished, what you actually have there is downright awful.

* The code nobodynews posted performs a copy-construction of each inner vector which results in never having to grow the vector because it starts out at the ideal size. Your code on the other hand causes the vector to grow when it reaches a certain size, and it will grow the capacity to probably larger than you need it for.

Do it the right way. Taking the important bits from nobodynews' code and inserting them into yours produces this:
int x = 11, y = 11;
std::vector< std::vector<bool> > mv(x, std::vector<bool>(y, false));
Now that's how you do it properly!

#2iMalc

Posted 17 December 2012 - 01:43 AM

Here is the finished product.

vector<vector<bool>> mv;
int x = 11;
int y = 11;
for(int i=0;i<x;i++) {
  mv.push_back(vector<bool>());
  for(int j=0;j<y;j++) {
   mv[i].push_back(new bool);
  }
}

Whoa hold on a sec there. You're dynamically allocating each bool. That is not a "finished product", and is not right at all. You would simply need to push_back the value, i.e. false. It's only compiling due to a pointer being able to be implicity converted to a bool, with the value 'true' in this case since the pointer isn't NULL. So it leaks like a sieve. It's also much longer, and much less efficient in both memory and time taken, even if you ignore the masses of leaks it causes. Nope, far from being finished, what you actually have there is downright awful.

The code nobodynews posted performs a copy-construction of each inner vector which results in never having to grow the vector because it starts out at the ideal size. Your code on the other hand causes the vector to grow when it reaches a certain size, and it will grow the capacity to probably larger than you need it for.

Do it the right way. Taking the important bits from nobodynews' code and inserting them into yours produces this:
int x = 11, y = 11;
std::vector< std::vector<bool> > mv(x, std::vector<bool>(y, false));
Now that's how you do it properly!

#1iMalc

Posted 17 December 2012 - 01:42 AM

Here is the finished product.

vector<vector<bool>> mv;
int x = 11;
int y = 11;
for(int i=0;i<x;i++) {
  mv.push_back(vector<bool>());
  for(int j=0;j<y;j++) {
   mv[i].push_back(new bool);
  }
}

Whoa hold on a sec there. You're dynamically allocating each bool. That is not a "finished product", and is not right at all. You would simply need to push_back the value, i.e. false. It's only compiling due to a pointer being able to be implicity converted to a bool, with the value 'true' in this case since the pointer isn't NULL. So it leaks like a sieve. It's also much longer, and much less efficient in both memory and time taken, even if you ignore the masses of leaks it causes. Nope, far from being finished, what you actually have there is downright awful.

The code nobodynews posted performs a copy-construction of each inner vector which results in never having to grow the vector because it starts out at the ideal size. Your code on the other hand causes the vector to grow when it reaches a certain size, and it will grow the capacity to probably larger than you need it for.

Do it the right way. Taking the two important bits from nobodynews' code and inserting them into yours produces this:
int x = 11, y = 11;
std::vector< std::vector<bool> > mv(x, std::vector<bool>(y, false));
Now that's how you do it!

PARTNERS