Topic: Reserve(n) for unordered containers reserves for n-1 elements.
Author: Daniel James <dnljms@gmail.com>
Date: Thu, 3 May 2012 11:06:40 -0700 (PDT)
Raw View
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-submit@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?=<daniel.kruegler@googlemail.com>
Date: Fri, 4 May 2012 11:24:27 -0700 (PDT)
Raw View
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
--
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]