What happens to a vector when one or more of its elements change size?
To elaborate,
#include <string>
#include <vector>
using namespace std;
int main() {
vector<string> v;
v.push_back("first string");
v.push_back("2nd string");
v[0] += " has increased in size";
}
what could/would happen at “v[0] += …”? Would there be a mass reallocation, to keep the memory contiguous in both string and vector?
There would be no memory re-allocation in the
vector, and there almost certainly will be a reallocation for thestring, although the standard does not say anything about that. The string is like a vector in that it keeps the content separately from the string object itself.