Quote Originally Posted by ProgrammerC++ View Post
How does a vector know it goes out of scope?
How does any object you create knows it goes out of scope? There is nothing magical about vector -- all objects have a destructor that gets invoked automatically when the object goes out of scope.

Do you know what a destructor is and when it's called? If not, I suggest you read up on basic C++ fundamentals.

All objects that are declared have a scope, block scope, function scope, file scope, etc. When the object goes out of scope, the destructor is automatically invoked. The object doesn't "know" when it is or is not in scope, as it doesn't need to. Everything is taken care of by the language.
Code:
void f()
{
    int a;
}  // when this function is done, a is no longer in scope.
Do you understand this concept?

Regards.

Paul McKenzie