I need help with vectors of vectors.

Started by
13 comments, last by black_darkness 11 years, 4 months ago
Hello. I am trying to something like a 2d array of bools (ex: bool var[11][11]). But using vectors instead.



I think that this doesn't work because you cannot put a vector in data type bool.

#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<vector<bool>> mv;
int x = 11;
int y = 11;
for(int i=0;i<x;i++) {
mv.push_back(vector<bool>());
cout<<"Creating x["<<mv.size()-1<<"]\n";
for(int j=0;j<y;j++) {
mv.push_back(vector<bool>());
}
}
return 0;
}


But in order to create a dynamic function that takes x and y and creates a 2d vector of bools I have no idea where to begin.
Advertisement
What do you mean by "doesn't work"? What error message do you get?

What do you mean by "doesn't work"? What error message do you get?


I'm getting this error
error C2664: 'std::vector<_Ty,_Ax>::push_back' : cannot convert parameter 1 from 'std::vector<_Ty,_Ax>' to 'bool'

Btw that error corresponds to line 13.
In line 13, you should just be pushing a bool, not a vector of bools. You would only push a vector in that inner loop if you were implementing a vector of vectors of vectors of bools (a 3D array, in other words).
The error is saying exactly the issue. You're pushing back a vector of bools and that code is expecting a bool. Try changing line 13 to this:

mv.push_back(bool());

Of course a better solution is to make this function:


std::vector<std::vector<bool>> Create2DVectorOfBools(int x, int y)
{
std::vector<bool> temp(y, false);
std::vector<std::vector<bool>> mv(x, temp);

return mv;
}


What this does is first creates a 1d vector of bools of length y and initializes every element in it to 'false' (see here for info on every constructor of std::vector http://msdn.microsoft.com/en-us/library/zzw4bwhd.aspx )
Then it creates a vector of vector of bools of length x and initializes every element in it to 'temp' (which, as stated, was initialized to length y).
Then it returns a copy of the resulting 2d vector.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Thank you both. I see the problem now. I was having trouble conceptualizing this. Now I got it. Thank you.

@nobodynews that is a good function. I was planning on creating a function later. Thanks for the good function.
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);
}
}


Will this handle the garbage collecting?

for (unsigned int i=0;i<mv.size();i++) {
mv.erase(mv.begin());
}
mv.clear() would be sufficient to clear everything.

mv.clear() would be sufficient to clear everything.


Thank you.

BTW. I found this code just now for visual studio compilers. src http://www.david-amador.com/2010/10/tracking-memory-leaks-in-visual-studio/

It detects memory leaks. I had a ton.
Do you recommend this as a good way for me to test for errors in the future?


#ifdef _DEBUG
int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
flag |= _CRTDBG_LEAK_CHECK_DF; // Turn on leak-checking bit
_CrtSetDbgFlag(flag);
#endif

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!
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement