Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reserve(n) for unordered containers reserves for n-1 elements.

71 views
Skip to first unread message

Daniel James

unread,
May 3, 2012, 2:06:40 PM5/3/12
to
Hello,

When calling 'x.reserve(n)' for an unordered container I'd expect to
be able to insert n elements without invalidating iterators. But as
the standard is written, the guarantee only holds for n-1 elements.
For a container with max_load_factor of 1, reserve(n) is equivalent to
rehash(ceil(n/1)), which is rehash(n). rehash(n) requires that the
bucket count is >= n, so it can be n. The rule is that insert shall
not affect the validity of iterators if (N + n) < z * B. But for this
case the two sides of the equation are equal, so insert can affect the
validity of iterators. Is that a mistake?

Daniel


--
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use mailto:std-cpp...@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

Daniel Krügler

unread,
May 4, 2012, 2:24:27 PM5/4/12
to
Am 03.05.2012 20:06, schrieb Daniel James:
> When calling 'x.reserve(n)' for an unordered container I'd expect to
> be able to insert n elements without invalidating iterators. But as
> the standard is written, the guarantee only holds for n-1 elements.
> For a container with max_load_factor of 1, reserve(n) is equivalent to
> rehash(ceil(n/1)), which is rehash(n). rehash(n) requires that the
> bucket count is>= n, so it can be n. The rule is that insert shall
> not affect the validity of iterators if (N + n)< z * B. But for this
> case the two sides of the equation are equal, so insert can affect the
> validity of iterators. Is that a mistake?

I tend to say that this is indeed a mistake. Please submit a library
defect to the email address referenced in the reply-to field of

http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html

Thanks& Greetings from Bremen,

Daniel Krügler
0 new messages