O'Reilly logo

C++ Primer, Fifth Edition by Barbara E. Moo, Josée Lajoie, Stanley B. Lippman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required