Title
vector::resize() missing efficiency guarantee
Status
nad
Section
[vector.capacity]
Submitter
David Abrahams

Created on 2009-10-24.00:00:00 last changed 164 months ago

Messages

Date: 2010-10-21.18:28:33

Rationale:

The description in terms of push_back led some to believe that one could expect the exact same growth pattern from both resize and push_back (e.g.) which could lead to sub-optimal implementations. Additionally, [vector], p1 includes a statement that this container "supports (amortized) constant time insert and erase operations at the end;", therefore addressing the concern of this issue.

Date: 2010-10-21.18:28:33

Proposed resolution:

In [vector.capacity]/10, change

void resize(size_type sz);

Effects: If sz < size(), equivalent to erase(begin() + sz, end());. If size() < sz, appends sz - size() default constructed elements to the sequence equivalent to sz - size() consecutive evaluations of push_back(T()).

Date: 2009-11-10.00:00:00

[ 2009-11-10 Howard adds: ]

Moved to Tentatively NAD after 5 positive votes on c++std-lib. Rationale added below.

Date: 2009-10-24.00:00:00

If v is a vector, I think repeated calls to v.resize( v.size() + 1 ) should be amortized O(1), but it's not clear that's true from the text of the standard:

void resize(size_type sz);

Effects: If sz < size(), equivalent to erase(begin() + sz, end());. If size() < sz, appends sz - size() default constructed elements to the sequence.

Seems to me if we used push_back instead of appends, we might be giving the guarantee I'd like. Thoughts?

History
Date User Action Args
2010-10-21 18:28:33adminsetmessages: + msg1299
2010-10-21 18:28:33adminsetmessages: + msg1298
2010-10-21 18:28:33adminsetmessages: + msg1297
2009-10-24 00:00:00admincreate