How does adding and removing elements “rescale” the data? How is the size of the vector calculated (I believe it is kept track of)? Any other additional resources to learn about vectors would be appreciated.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In terms of sizing, there are two values of interest for a
std::vector:size, andcapacity(accessed via.size()and.capacity())..size()is the number of elements that are contained in the vector, whereas.capacity()is the number of elements that can be added to the vector, before memory will be re-allocated.If you
.push_back()an element, size will increase by one, up until you hit the capacity. Once the capacity is reached, most (all?) implementations, re-allocate memory, doubling the capacity.You can reserve a capacity using
.reserve(). For example:Reallocations of memory would occur at lines 4, 5, and 7.