In case we push an element at (vector size + 1) index, does the vector size always gets doubled, or will it just increase by the number of elements pushed?
Does vector size always gets doubled when increasing it's size?
hello @niveditha_palli
there are two terms .
a) vector size -> it is equal to number of elements in the vector.
b) vector capacity-> total space vector is taking.
when vector size exceeds its capacity then vector capacity get doubled.
ex->
int main(){
vector<int > vec;
vec.push_back(1);
vec.push_back(1);
vec.push_back(1);
cout<<"size of vector"<<vec.size()<<endl;
cout<<"capacity of vector"<<vec.capacity()<<endl;
return 0;
}
currently size is 3 and capacity is 4.
Now if we push back one more element,
now size is: 4 capacity is 4
now if we try to insert one more element in vector then size will become 5 but capacity will become 8.
is this what u want to know?
Thank you, understood it much better.
Yes, that was my question.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.