Table of contents
Vector moves the element position:
In C++, std::vector
is a self-managing container that automatically handles memory allocation and deallocation. When you std::vector
add elements in it, it automatically allocates enough memory to store the elements. When std::vector
destroyed (for example, when it goes out of scope, or it is a member of an object and that object is destroyed), it automatically destroys all elements and frees the memory used to store the elements. Therefore, you usually don't need to worry std::vector
about memory leaks.
However, if you std::vector
're storing pointers in , and those pointers point to dynamically allocated memory, then you need to make sure that the memory is properly freed when it's no longer needed. std::vector
does not automatically delete the memory pointed to by its elements, so you need to manually delete these memory, or use smart pointers (such as std::unique_ptr
or std::shared_ptr
) to automatically manage this