用许多块实现的向量并且没有调整大小副本

发布于 2025-01-03 14:14:15 字数 138 浏览 0 评论 0原文

我想知道是否可以实现一个类似 stl 的向量,其中存储是在块中完成的,而不是分配一个更大的块并从原始块复制,您可以将不同的块保留在不同的位置,并重载运算符[]和迭代器的运算符++,以便向量的用户不知道块不连续。

当超出现有容量时,这可以保存副本。

I'm wondering if it would be possible to implement an stl-like vector where the storage is done in blocks, and rather than allocate a larger block and copy from the original block, you could keep different blocks in different places, and overload the operator[] and the iterator's operator++ so that the user of the vector wasn't aware that the blocks weren't contiguous.

This could save a copy when moving beyond the existing capacity.

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

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

发布评论

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

评论(2

挽你眉间 2025-01-10 14:14:15

您将寻找 std::deque

请参阅 GotW #54 使用向量和双端队列

在大多数情况下,更喜欢使用双端队列(有争议)

包含用于演示行为的基准测试

最新的 C++11 标准表示:

§ 23.2.3 序列容器

[2] 序列容器为程序员提供了不同的复杂性权衡,因此应相应地使用。
矢量或数组是默认应使用的序列容器类型。列表或转发列表
当序列中间频繁插入和删除时应使用。双端队列是
当大多数插入和删除发生在开头或结尾时选择的数据结构
序列。

常见问题解答 >前奏曲的角落>矢量还是双端队列? (中级) 说:

  • 向量只能有效地将项目添加到末尾,任何在向量中间或开头插入项目的尝试都可能而且通常效率非常低。 双端队列可以在恒定时间内在开头和结尾插入项目,O(1),这是非常好的。在中间插入仍然效率低下,但如果需要此类功能,则应使用列表。双端队列在前面插入的方法是push_front(),也可以使用insert()方法,但push_front更清晰。

  • 就像插入一样,向量前面的擦除效率很低,但是双端队列也提供从前面进行恒定时间的擦除

  • 双端队列可以更有效地使用内存。考虑内存碎片,向量需要 N 个连续的内存块来保存其项目,其中 N 是项目的数量,块是单个项目的大小。如果向量需要 5 或 10 兆字节的内存,但可用内存碎片化到没有 5 或 10 兆字节的连续内存,这可能会成为问题。 双端队列不存在这个问题,如果没有足够的连续内存,双端队列将使用一系列较小的块。

[...]

You would be looking for std::deque

See GotW #54 Using Vector and Deque

In Most Cases, Prefer Using deque (Controversial)

Contains benchmarks to demonstrate the behaviours

The latest C++11 standard says:

§ 23.2.3 Sequence containers

[2] The sequence containers offer the programmer different complexity trade-offs and should be used accordingly.
vector or array is the type of sequence container that should be used by default. list or forward_list
should be used when there are frequent insertions and deletions from the middle of the sequence. deque is
the data structure of choice when most insertions and deletions take place at the beginning or at the end of
the sequence.

FAQ > Prelude's Corner > Vector or Deque? (intermediate) Says:

  • A vector can only add items to the end efficiently, any attempt to insert an item in the middle of the vector or at the beginning can be and often is very inefficient. A deque can insert items at both the beginning and then end in constant time, O(1), which is very good. Insertions in the middle are still inefficient, but if such functionality is needed a list should be used. A deque's method for inserting at the front is push_front(), the insert() method can also be used, but push_front is more clear.

  • Just like insertions, erasures at the front of a vector are inefficient, but a deque offers constant time erasure from the front as well.

  • A deque uses memory more effectively. Consider memory fragmentation, a vector requires N consecutive blocks of memory to hold its items where N is the number of items and a block is the size of a single item. This can be a problem if the vector needs 5 or 10 megabytes of memory, but the available memory is fragmented to the point where there are not 5 or 10 megabytes of consecutive memory. A deque does not have this problem, if there isn't enough consecutive memory, the deque will use a series of smaller blocks.

[...]

痕至 2025-01-10 14:14:15

是的,这是可能的。

你知道绳子吗?这就是你所描述的,对于字符串(大字符串==绳子,明白这个笑话吗?)。 Rope 不是标准的一部分,但出于实用目的:它可以在现代编译器上使用。您可以使用它来表示文本编辑器的完整内容。

请看这里:STL 绳索 - 何时何地使用

以及永远记住:(

  • 性能)优化的第一条规则是:不要这样做
  • 第二条规则(仅适用于专家):现在不要这样做。

Yes it's possible.

do you know rope? it's what you describe, for strings (big string == rope, got the joke?). Rope is not part of the standard, but for practical purposes: it's available on modern compilers. You could use it to represent the complete content of a text editor.

Take a look here: STL Rope - when and where to use

And always remember:

  • the first rule of (performance) optimizations is: don't do it
  • the second rule (for experts only): don't do it now.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文