The reallocate Member
Using this information, we can now write our reallocate member. We’ll start by calling allocate to allocate new space. We’ll double the capacity of the StrVec each time we reallocate. If the StrVec is empty, we allocate room for one element:
void StrVec::reallocate() { // we'll allocate space for twice as many elements as the current size auto newcapacity = size() ? 2 * size() : 1; // allocate new memory auto newdata = alloc.allocate(newcapacity); // move the data from the old memory to the new auto dest = newdata; // points to the next free position in the new array auto elem = elements; // points to the next element in the old array for (size_t i = 0; i != size(); ++i) alloc.construct(dest++, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access