Warm tip: This article is reproduced from stackoverflow.com, please click
c++

Does C++ vector copy constructor and assignment operator also copies reserved space?

发布于 2020-04-03 23:21:42

Copy constructor is called

vector<int> v0;
v0.reserve(3);
vector<int> v1 = v0;

Will v1 also has reserved space of 3?

Assignment operator is called

vector<int> v0;
v0.reserve(3);
vector<int> v1;
v1 = v0;

Will v1 also has reserved space of 3?

Questioner
Harry Kane
Viewed
31
Evgeny 2020-01-31 19:43

The standart doesn't tell anything about additional reservation after copying. So, reserved space is realization specific. So, you shouldn't rely on reserved space after copy.