Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shrinking a vector

Tags:

c++

stl

vector

I've got a problem with my terrain engine (using DirectX).

I'm using a vector to hold the vertices of a detail block. When the block increases in detail, so the vector does.

BUT, when the block decreases its detail, the vector doesn't shrink in size.

So, my question: is there a way to shrink the size of a vector? I did try this:

vertexvector.reserve(16);
like image 419
Brammie Avatar asked Feb 25 '09 16:02

Brammie


People also ask

What does resizing a vector do?

vector::resize() The function alters the container's content in actual by inserting or deleting the elements from it. It happens so, If the given value of n is less than the size at present then extra elements are demolished.

Does resizing a vector delete data?

Resizing a vector doesn't destroy the values stored in the vector (except for those beyond the new size when shrinking, of course), however growing a vector beyond its capacity will copy (or, in C++11, move) them to a new place, thus invalidating and iterators, pointers or references to those elements.

How vector increases its size?

By default, the vector increases its capacity by double. However, if an increment is specified in its constructor, Vector will grow in accordance with it in each allocation cycle.

What is shrink C++?

The C++ function std::deque::shrink() requests the deque to reduce its capacity to fit its size.


3 Answers

If you pop elements from a vector, it does not free memory (because that would invalidate iterators into the container elements). You can copy the vector to a new vector, and then swap that with the original. That will then make it not waste space. The Swap has constant time complexity, because a swap must not invalidate iterators to elements of the vectors swapped: So it has to just exchange the internal buffer pointers.

vector<vertex>(a).swap(a);

It is known as the "Shrink-to-fit" idiom. Incidentally, the next C++ version includes a "shrink_to_fit()" member function for std::vector.

like image 126
Johannes Schaub - litb Avatar answered Oct 05 '22 03:10

Johannes Schaub - litb


The usual trick is to swap with an empty vector:

vector<vertex>(vertexvector.begin(), vertexvector.end()).swap(vertexvector);
like image 42
David Thornley Avatar answered Oct 05 '22 03:10

David Thornley


The reserved memory is not reduced when the vector size is reduced because it is generally better for performance. Shrinking the amount of memory reserved by the vector is as expensive as increasing the size of the vector beyond the reserved size, in that it requires:

  1. Ask the allocator for a new, smaller memory location,
  2. Copy the contents from the old location, and
  3. Tell the allocator to free the old memory location.

In some cases, the allocator can resize an allocation in-place, but it's by no means guaranteed.

If you have had a very large change in the size required, and you know that you won't want that vector to expand again (the principal of locality suggests you will, but of course there are exceptions), then you can use litb's suggested swap operation to explicitly shrink your vector:

vector<vertex>(a).swap(a);
like image 37
Matthew Xavier Avatar answered Oct 05 '22 01:10

Matthew Xavier