Empty vector of type <stuff*>

Posted by bo23 on Stack Overflow See other posts from Stack Overflow or by bo23
Published on 2011-01-18T00:49:05Z Indexed on 2011/01/18 0:53 UTC
Read the original article Hit count: 117

Filed under:

I have a vector populated with objects:

std::vector<Stuff*> stuffVector;

and am trying to delete all elements of it using a cleanup function

void CleanUp()
{
    for (std::vector<Stuff*>::size_type i = 0 ; i < stuffVector.size() ; i++)
    {
        stuffVector.erase(stuffVector.begin()+i);
    }
    cout << stuffVector.size() << endl;
    if (stuffVector.size() == 0) cout << "Vector Emptied" << endl;
}

This always reports back with a size of however many objects are in the vector, and doesn't actually seem to delete anything at all. It's odd as a similar function works elsewhere to delete a specific object from the vector:

void DestroyStuff()
{
    if (stuffVector.size() > 1)
    {
        for (std::vector<Stuff*>::size_type i = 0 ; i < stuffVector.size() ; i++ )
        {
            if(stuffVector[i]->CanDestroy())
            {
                stuffVector.erase (stuffVector.begin()+i);
            }
        }
    }
}

The above works fine, but CleanUp() does not. Why might this be happening?

© Stack Overflow or respective owner

Related posts about c++