I wonder why you even bothered to ask, if you already knew about using
delete
. If your reason for asking is that you want to know how to avoid having to take care of memory allocation and deallocation, just put that vector on the stack, rather than creating it on the heap:
{
vector<unsigned int> vecnDigit;
vecnDigit.push_back(2);
}
Note that instead of creating the vector on the heap, I just put the declaration and use of the vector into a code block, limiting it's scope. That way your use of that vector is still limited to the lines of code between your declaration, and the end of this block. But as it's declared on the stack, you don't need to take care of allocation or deallocation.