D
dwightarmyofchampions
I am having trouble with two-dimensional vectors. In my old code I had
a vector in my class definition that looked like this:
std::vector<ABC*> vec;
....which means I am declaring a vector whose elements will contain
pointers to ABC objects.
....and in my constructor I have:
// make sure vector is empty before populating it
vec.clear();
for (int i = 0; i < 5; i++)
{
ABC* abcobject1 = new ABC(i);
vec.push_back(abcobject1);
}
....and in my destructor I have:
for (std::vector<ABC*>::iterator it = vec.begin();
it != vec.end();
it++)
{
delete *it; *it = 0;
}
This is all well and good and appears to work OK. Now, let's suppose
that I want to make vec be a two-dimensional vector instead of a one-
dimensional vector. That is, the definition in my header file now
looks like this:
// vec is a vector of a vector of pointers to ABC objects
std::vector< std::vector<ABC*> > vec;
OK, now here's where I'm messing up. My constructor looks like this:
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 5; j++)
{
ABC* abcobject1 = new ABC(j);
vec.push_back(abcobject1);
}
}
When the loop first iterates (i and j both equal zero) how can it
push_back an item onto vec[0] when vec[0] itself doesn't exist yet? I
think there needs to be a vec.push_back(...) line somewhere earlier,
but I don't know where and what should be pushed back. Can someone
help me?
Oh, and my destructor looks like this:
for (std::vector< std::vector<ABC*> >::iterator itOuter = vec.begin();
itOuter != vec.end();
itOuter++)
{
for (std::vector<ABC*>::iterator itInner = (*itOuter).begin();
itInner != (*itOuter).end();
itInner++)
{
delete *itInner; *itInner = 0;
}
}
I have no idea if that's correct, since I haven't gotten that far yet.
a vector in my class definition that looked like this:
std::vector<ABC*> vec;
....which means I am declaring a vector whose elements will contain
pointers to ABC objects.
....and in my constructor I have:
// make sure vector is empty before populating it
vec.clear();
for (int i = 0; i < 5; i++)
{
ABC* abcobject1 = new ABC(i);
vec.push_back(abcobject1);
}
....and in my destructor I have:
for (std::vector<ABC*>::iterator it = vec.begin();
it != vec.end();
it++)
{
delete *it; *it = 0;
}
This is all well and good and appears to work OK. Now, let's suppose
that I want to make vec be a two-dimensional vector instead of a one-
dimensional vector. That is, the definition in my header file now
looks like this:
// vec is a vector of a vector of pointers to ABC objects
std::vector< std::vector<ABC*> > vec;
OK, now here's where I'm messing up. My constructor looks like this:
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 5; j++)
{
ABC* abcobject1 = new ABC(j);
vec.push_back(abcobject1);
}
}
When the loop first iterates (i and j both equal zero) how can it
push_back an item onto vec[0] when vec[0] itself doesn't exist yet? I
think there needs to be a vec.push_back(...) line somewhere earlier,
but I don't know where and what should be pushed back. Can someone
help me?
Oh, and my destructor looks like this:
for (std::vector< std::vector<ABC*> >::iterator itOuter = vec.begin();
itOuter != vec.end();
itOuter++)
{
for (std::vector<ABC*>::iterator itInner = (*itOuter).begin();
itInner != (*itOuter).end();
itInner++)
{
delete *itInner; *itInner = 0;
}
}
I have no idea if that's correct, since I haven't gotten that far yet.