Re: Should an iterator be valid after appending to a Glib::ustring?



On 13 March 2017 at 21:46, Daniel Boles <dboles src gmail com> wrote:
This is a pretty obvious "no". If the operation can cause a reallocation, that's a very strong hint, so if the container does not explicitly state that it does NOT invalidate iterators.... then it does

I really meant 'increase the capacity', not 'reallocation', if we use 'reallocation' in the sense of moving existing elements, not just allocating other memory for new ones. So reallocation in this sense should always invalidate iterators. And since ustring (and std::string) is a random-access container, it must have contiguous memory, so any increase in capacity must cause a reallocation. So, any insert/append or anything else that adds elements.

but there's a point: if you had previously reserve()d enough capacity, beyond the current size, for the insert point and length, then as long as you stay within the capacity you had, your iterators should remain valid, as reallocation is guaranteed not to occur in such cases.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]