If you use push_back
or insert
, yes, the vector resizes. Here is a demo:
#include<iostream>
It outputs as:
1 2 3 Current size 3
Now remember, if you do a[3] = 5
. It will not resize your vector automatically.
You can also manually resize the vector if you want. To demonstrate, add the following code to the code above.
a.resize(6); for (int value : a) { cout << a << " "; } cout << endl << "Current size " << a.size() << endl;
Now it will output:
1 2 3 Current size 3 1 2 3 0 0 0 Current size 6
I think you got your answer.
source share