Pretty simple question, but why is it that when you call
std::vector<int> vec;
vec.reserve(100);
vec[89] = 99; // vector subscript out of range.-
You are not allowed to access elements 0-99 of the vector? Doesn't make a lot of sense considering that reserving increased capacity of the vector (allocated a new array of that size) and such you should be able to access those elements even though they would be empty or null to start.
I understand that if you do it the following way:
std::vector<int> vec(99, 0);
vec[89] = 99;
You would be allowed to access the elements because they will be initialized to 0, however, you can't resize a vector with default values like this more than once other than initially through the constructor.
With arrays, one can do:
int[] arr = new int[100];
arr[89] = 99;
Granted you would have to write the resizing yourself.
arr1 = new int[200]
memcpy (arr1, arr, 100);
arr = arr1;
arr[90] = 100;
Is there any way to treat std::vector
like arrays that resize themselves once you reach a certain capacity?