缩小向量

发布于 2024-07-14 03:06:38 字数 208 浏览 3 评论 0原文

我的地形引擎(使用 DirectX)有问题。

我正在使用向量来保存细节块的顶点。 当块的细节增加时,矢量也会增加。

但是,当块减少其细节时,矢量的大小不会缩小。

所以,我的问题是:有没有办法缩小向量的大小? 我确实尝试过这个:

vertexvector.reserve(16);

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

且行且努力 2024-07-21 03:06:38

如果从向量中弹出元素,它不会释放内存(因为这会使容器元素中的迭代器无效)。 您可以将向量复制到新向量,然后将其与原始向量交换。 这样就不会浪费空间。 交换具有恒定的时间复杂度,因为交换不得使交换向量的元素的迭代器无效:因此它必须只交换内部缓冲区指针。

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

它被称为“Shrink-to-fit”习语。 顺便说一句,下一个 C++ 版本包括 std::vector 的“shrink_to_fit()”成员函数。

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.

时光礼记 2024-07-21 03:06:38

通常的技巧是与空向量交换:

vector<vertex>(vertexvector.begin(), vertexvector.end()).swap(vertexvector);

The usual trick is to swap with an empty vector:

vector<vertex>(vertexvector.begin(), vertexvector.end()).swap(vertexvector);
渔村楼浪 2024-07-21 03:06:38

当向量大小减小时,保留的内存不会减少,因为它通常对性能更好。 缩小向量保留的内存量与将向量的大小增加到超出保留大小一样昂贵,因为它需要:

  1. 向分配器请求一个新的、更小的内存位置,
  2. 从旧位置复制内容,然后
  3. 告诉分配器释放旧的内存位置。

在某些情况下,分配器可以就地调整分配大小,但这绝不是保证的。

如果您对所需的大小进行了很大的更改,并且您知道您不会希望该向量再次扩展(局部性原理表明您会这样做,但当然也有例外) ,那么您可以使用 litb 建议的交换操作来显式缩小向量:

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

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);
献世佛 2024-07-21 03:06:38

有一个用于此目的的成员函数,shrink_to_fit。 它比大多数其他方法更有效,因为它只会在需要时分配新内存并进行复制。 详细信息在这里讨论,
是shrink_to_fit将 `std::vector` 容量减少到其大小的正确方法?

如果您不介意 libc 分配函数 realloc 甚至更有效,它不会在收缩时复制数据,只需标记额外的内存是空闲的,如果您增加内存并且有空闲的内存,它会将所需的内存标记为已使用,并且也不复制。 但要小心,您正在从 C++ stl 模板转移到 C void 指针,并且需要了解指针和内存管理的工作原理,现在许多人不赞成它作为错误和内存泄漏的来源。

There is a member function for this, shrink_to_fit. Its more efficient than most other methods since it will only allocate new memory and copy if there is a need. The details are discussed here,
Is shrink_to_fit the proper way of reducing the capacity a `std::vector` to its size?

If you don't mind the libc allocation functions realloc is even more efficient, it wont copy the data on a shrink, just mark the extra memory as free, and if you grow the memory and there is memory free after it will mark the needed memory as used and not copy either. Be careful though, you are moving out of C++ stl templates into C void pointers and need to understand how pointers and memory management works, its frowned upon by many now adays as a source for bugs and memory leaks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文