I just found out that std::vector<T>::resize
"doubles" its capacity even when resizing to one element above the current size:
std::vector<int> v(50);
v.resize(51);
std::cout << v.capacity() << std::endl;
This program outputs 100 with GCC and Clang, and 75 with Visual C++. However, when I switch from resize
to reserve
:
std::vector<int> v(50);
v.reserve(51);
std::cout << v.capacity() << std::endl;
The output is 51 with all three compilers.
I wonder why implementations use a different expansion strategy for resize
and reserve
. It seems inconsistent, and I would expect the same behavior here.
I am just adding a link to a motivation for my question, where the impact on performance is reported: Why are C++ STL vectors 1000x slower when doing many reserves?
Adding a quote from C++11 Standard to clarify requirements for reserve
; §23.3.6.3(2):
After
reserve()
,capacity()
is greater or equal to the argument ofreserve
if reallocation happens...
Some additional thoughts: From C++11 Standard:
Complexity: The complexity is linear in the number of elements inserted plus the distance to the end of the vector.
Which, effectively, implies constant (amortized) complexity for inserting a single element at the end. However, this applies only for vector modifiers, such as push_back
or insert
(§23.3.6.5).
resize
is not listed among modifiers. It's listed in §23.3.6.3 vector
capacity section. And, there are no complexity requirements for resize
.
However, in the vector
overview section (§23.3.6.1), there is written:
it (
vector
) supports (amortized) constant time insert and erase operations at the end
The question is whether resize(size()+1)
is considered to be "insertion at the end".
resize
doubles the capacity, whilereserve
does not". This question asks why. (I don't think that Google would find you the other question if you asked this one.)