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++, ...

Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.