I need help with vectors of vectors.

Started by
13 comments, last by black_darkness 11 years, 4 months ago

[quote name='black_darkness' timestamp='1355718293' post='5011515']
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.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!
[/quote]

I feel evil now. Thanks. I was wondering why the program wouldn't work. I got distracted trying to solve my memory leaks and couldn't fix them. Then I came back and saw your post. I appreciate it. It doesn't help that cplusplus.com is down, which is my reference of choice for problems.
Advertisement
Once you are using vectors, you shouldn't write `new' anywhere: The vector is doing the memory allocations for you. If you feel tempted to use `new' again, ask here to make sure you really have a good reason (hint: unless you are trying to get polymorphic behavior, you won't need it).
Are you sure you actually want to use a std::vector<bool> ?

http://www.gotw.ca/publications/N1185.pdf
http://www.gotw.ca/p...tions/N1211.pdf
My test I wrote is compiling properly but It crashes.

When it crashes it tells me
Expression: vector<bool> iterator not dereferencable.

And
Standard C++ libraries are out of range &&0

What I am trying to get the test to do is make a class where I can input x,y, and a string. X and Y will determine width and height. And the string will determine if the coordinate is passable or impassable. I keep thinking I solve the problem but I keep getting an error. I am starting to get lost because I keep having to correct mistakes and I forget what mistakes I made. I am getting lost I feel like I am in a vicious cycle. Any help would be appreciated.


vector<vector<bool>> make_map(int x, int y, string grid)
{
vector<vector<bool>> mv(x, vector<bool>(y, false));
int counter = 0;
for(int i=0;i<x;i++) {
for(int j=0;i<y;j++) {
if(grid[counter]=='X') {
mv[j]=false;
}
else if(grid[counter]=='O') {
mv[j] = true;
}
counter++;
}
}
return mv;
}

int main()
{
int mx = 3;
int my = 3;
string grid = "XOXOOOOXO";
vector<vector<bool>> mv = make_map(mx,my,grid);
for(int i=0;i<mx;i++) {
for(int j=0;j<my;j++) {
cout<<"X: "<<i<<"Y: "<<j<<"= "<<mv[j]<<"\n";
}
}
return 0;
}

Well I solved this one myself. The problem was on line 6. I had an i instead of a j. Now everything works. I laughed like a maniac. I am very pleased with this.

This topic is closed to new replies.

Advertisement