I have declared:
vector<int> * part = new vector<int>[indmap];
Ok, I know it: “why don’t you declare a vector of vector?”.
The next time I will do so, but now I would like to understand something more about vectors which I’m not able to solve.
Now I want to release all resources, what have I to do?
delete[] part;
But before deleting the array what should I do to delete all the vector objects?
When you use
delete, the destructor of the pointed-to object is called before de-allocation (do this, when you have created an object usingnew).Similarly, the destructors of all created objects are called when you use the array version
delete[](do this, when you have allocated an array of objects usingnew[]).The destructor of a
std::vector<T>automatically calls the destructors of the contained objects.So, as Joachim Pileborg has pointed out in his answer, you need to take care of deleting the vector’s objects manually, only when you have raw pointers in there that point to dynamically allocated objects (e.g., objects allocated by
new) and you are responsible for their deletion (i.e., you own them). Raw pointers don’t have destructors that would destroy the pointed-to objects, so you would have to iterate the vector to delete the objects manually in this case.Otherwise the vector can simply be destroyed (or array-deleted, in this case).
As a rule of thumb:
deletememory you have allocated usingnew.delete[]memory you have allocated usingnew[].For reference: