std::vector<AClass> vect;
AClass Object0, Object1, Object2, Object3, Object4;
vect.push_back(Object0); // 0th
vect.push_back(Object1); // 1st
vect.push_back(Object2); // 2nd
vect.push_back(Object3); // 3rd
vect.push_back(Object4); // 4th
Question 1 (Shrinking): Is it guarantied that the 0th, 1st and 2nd elements are protected (i.e.; their values do not change) after resizing this vector with this code: vect.resize(3)
?
Question 2 (Expanding): After expanded this vector by the code vect.resize(7)
;
a. Are the first 5 elements (0th through 4th) kept unchanged?
b. What happens to the newly added two elements (5th and 6th)? What are their default values?
Question 1: Yes, the standard says:
void resize(size_type sz);
If
sz < size()
, equivalent toerase(begin() + sz, end());
.
Question 2: If no resizing is required, yes. Otherwise, your elements will be copied to a different place in memory. Their values will remain unchanged, but those values will be stored somewhere else. All iterators, pointers and references to those objects will be invalidated. The default value is AClass()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With