September 12th, 2003, 09:00 PM
How to delete an empty vector
I am trying to delete empty STL vector objects. Can any one tell me what is a convenient way to do this?
September 13th, 2003, 04:12 AM
I'm assuming you mean deleting the vector itself, rather then removing vector elements.
When you declare a vector in your program it is declared within a scope, just like any other variable. The lifetime of the vector depends on how it is created.
If you create a global or static vector it will persist for the lifetime of your program.
If you create a vector using dynamic memory allocation (i.e. with new) then the pointer to it will cease to exist when it goes out of scope, and prior to that you should release the memory used for the vector with delete.
If you create a non-dynamic vector within a function it will cease to exist when it goes out of scope, i.e. when it reaches the closing brace of the enclosing block.
I hope that helps. If that doesn't make sense, please post some code to show how you're using the vector.