c++ - Why is string::resize linear in complexity? -
the string needs delete old memory, declare new memory buffer, both of o(1) in complexity. cplusplus.com reference says linear in time complexity of new string length. because old string needs copied over? if start empty string?
basically want string declared size n buffer, in o(1) time. possible?
every element of new string initialized, either moving old elements, or making copies of second argument of std::string::resize
(which defaults chart()
).
therefore number of initializations (new length).
one imagine future improved allocator allowed in-place adjustment of block size (like realloc
c library), additional memory might found contiguous old, , max(0, (new length) - (old length)) initializations needed. current allocator scheme doesn't support such.
Comments
Post a Comment