I just noticed that for vector push_back it is push back a reference to the element.
void push_back ( const T& x );
My question is does the memory layout changed after push_back?
For example, I have an array first which containing five elements and the layout is like this.
| | | | | |
| A1 | A2 | A3 | A4 | A5 |
Now I have a vector v
v.push_back(A3)
Now, how does the memory look like?
How does the vector store the elements here?
How does the vector access the element?
A vector stores by value not by reference.
When you re-add the same element, a copy will be stored at the end. If you do not want to make a copy of the values you are inserting into the
vector, then you should use pointers instead.Example:
vnow contains 2 different elements, one with an empty string, and one with a string which contains"hi". Both strings in the vector remain independent froms.Note: The internal implementation details of an STL container can vary, there is no guarantee that it will be implemented a certain way; however, the semantics of how an STL container works, will remain the same no matter what the internal implementation is.