Is capacity copied in a vector?
Take the following code:
std::vector<int> a;
a.reserve(65536);
std::vector<int> b(a); //NOTE: b is constructed from a
a.reserve(65536); // no reallocation
b.reserve(65536);
Is capacity copied? Will there be a reallocation on the last line? Does the standard say anything about this or is it silent?
Solution 1:
Is capacity copied?
In practice, no. I tested it online in Clang and GCC as well as MSVC and none of them copy the capacity.
Will there be a reallocation on the last line?
If the capacity is less than the argument to reserve (i.e. it doesn't get copied) then yes.
Does the standard say anything about this or is it silent?
No definitions for the copy constructor are provided in vector.cons. Instead we have to look at the container.requirements
X
denotes a container class containing objects of typeT
,a
andb
denote values of typeX
,u
denotes an identifier,r
denotes a non-const value of typeX
, andrv
denotes a non-const rvalue of typeX
.
X u(a)
X u = a;
Requires:
T
isCopyInsertable
intoX
(see below).post:
u == a
Now what does it mean for two containers to be equal?
a == b
==
is an equivalence relation.equal(a.begin(), a.end(), b.begin(), b.end())
In other words, since it doesn't require capacity
to be equal in the comparison, then there's no reason to copy the capacity
.
Solution 2:
Standard says nothing about preserving capacity when you call copy constructor. So you have no any guarantees about it.
But you can do the following trick, which swap a's and b's state, if you need preserving capacity in the copy only:
std::vector<int> a;
a.reserve(65536);
std::vector<int> b(a);
b.swap(a); // now b has a's state
assert(b.capacity() == 65536);
Solution 3:
No, capacity is not guaranteed to be preserved by a vector
copy construction.
You can do that as follows:
vector<int> b;
b.reserve( a.capacity() );
b = a;
Better encapsulated in a function.
Solution 4:
Well, a simple check like the following reveals that the capacity is not copied:
std::vector<int> a;
a.reserve(65536);
cout << "a.capacity is " << a.capacity() << endl; // prints 65536
std::vector<int> b(a); //NOTE: b is constructed from a
cout << "b.capacity is " << b.capacity() << endl; // prints 0
I believe that upon copying vector a
to b
, the capacity of b
is set to the size of a
in most compilers; though this is not guaranteed.